En este articulo aprenderas a crear un panel administrativo haciendo uso de Laravel 11 + Laravel breeze + Spatie permission, negando el acceso a usuarios que no sean administradores del sitio.
Laravel new admin
composer require spatie/laravel-permission
'providers' => [
// ...
Spatie\Permission\PermissionServiceProvider::class,
];
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider"
Php artisan migrate
use Spatie\Permission\Traits\HasRoles;
use HasRoles;
$middleware->alias([
'role' => \Spatie\Permission\Middleware\RoleMiddleware::class,
'permission' => \Spatie\Permission\Middleware\PermissionMiddleware::class,
'role_or_permission' => \Spatie\Permission\Middleware\RoleOrPermissionMiddleware::class
]);
use Spatie\Permission\Models\Role;
$role = Role::create(['name' => 'admin']);
$role = Role::create(['name' => 'cliente']);
php artisan make:middleware administrador
use Illuminate\Support\Facades\Auth; public function handle(Request $request, Closure $next): Response
{
if(auth()->check() && Auth::user()->hasRole('admin')){
return $next($request);
}
return redirect('/');
}
->withMiddleware(function (Middleware $middleware) {
$middleware->alias([
'admin' => \App\Http\Middleware\administrador::class,
]);
})
use App\Http\Middleware\administrador;
use App\Http\Controllers\admin\DashBoardController;
Route::middleware([administrador::class])->group(function () {
Route::get('/dashboard', [DashBoardController::class, 'index'])->name('dashboard');
Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
});
php artisan make:controller admin\DashBoardController
public function index(){
return view('dashboard');
}
])->assignRole('cliente');
<form method="POST" action="{{ route('logout') }}">
@csrf
<a href="route('logout')" onclick="event.preventDefault();
this.closest('form').submit();" class="text-black ring-transparent text-white">
{{ __('Log Out') }}
</a>
</form>
Etiquetas:
Gusto por las motos Harley Davidson, el rock n roll y la informática, creador de este blog para difusión del conocimiento libre, interesado en el mundo open source.
Configura una red VPN para tu negocio punto a punto con cisco
Como instalar y Configurar | Vue 3 Router | Laravel 9 | Vite | Vuetify
Como editar las rutas de autenticacion en Laravel Jetsream
How to Fix Error TPM Windows 11 Solución
Configura Https SSL Gratis con Cloudflare
Ir a otra seccion al hacer clic - Angular Scroll to element