Coder Social home page Coder Social logo

laravel-portal's Introduction

Laravel Portal

Portal Header

Latest Version on Packagist Build Status Quality Score Total Downloads

Easily create single routes that serve guests and authenticated users.

Why this pacakge?

Imagine a route/page you want to be visible by guests and users. Normally you'll create a route, for example Route::get('/', AppController::class), in this controller you'd do a check to see if the user is authenticated or not. Your controller might look something like this:

<?php

class AppController {
    public function __invoke()
    {
        if (Auth::check()) {
            return $this->dashboard();
        }
        
        return $this->guestPage();
    }
    
    public function guestPage()
    {
        $flights = \App\Flight::where('active', 1)
            ->orderBy('date', 'desc')
            ->take(10)
            ->get();
            
        return view('pages/guest/index', [
            'flights' => $flights,        
        ]);
    }
    
    protected function dashboard()
    {
        $flights = Auth::user()->flights->where('active', 1)
            ->orderBy('data', 'desc')
            ->take(5)
            ->get();
        
        return view('pages/auth/index', [
            'flights' => $flights,
        ]);
    }
}

This is a pretty basic example, but here you're returning a different view + data when the user is authenticated or not. Laravel Portal was created to eliminate the auth check and decouple this from your controllers. Your controllers (or methods) can be focussed only on the task they need to perform. This is done by adding a middleware and swapping the intended action based on the authentication.

Installation

You can install the package via composer:

composer require robertboes/laravel-portal

Usage

Using the config

Define your "route_actions" in the config. The array index maps to your "route name" and should contain at least a guest and authenticated action. These simply map to the controller method you want to call. An example looks like this:

'route_actions' => [
    'app' => [
        'guard' => 'web',
        'guest' => \App\Controllers\HomeController::class . '@__invoke',
        'auth'  => \App\Controllers\DashboardController::class . '@__invoke',
    ],
    'ajax.stats' -> [
        'guest' => 'App\Controllers\StatsController@global', 
        'auth'  => 'App\Controllers\StatsController@user',
    ],
],

Then you can reference to this config in your routing files. You only pass the path (url) and the route_action, for example "ajax.stats"

Route::portal('/', 'app');
Route::group(['middleware' => ['ajax']], function () {
    Route::portal('/ajax/stats', 'ajax.stats');
});

The user that visits the route your-laravel.app/ will automatically see the correct version. A guest user will only see the public page, a signed in user will see his/her dashboard. This works for any route, JSON routes and also works with any guard.

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.

laravel-portal's People

Contributors

robertboes avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.