Easily create single routes that serve guests and authenticated users.
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.
You can install the package via composer:
composer require robertboes/laravel-portal
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.
composer test
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
The MIT License (MIT). Please see License File for more information.
This package was generated using the Laravel Package Boilerplate.