laravel指定每个角色的路由

e4eetjau  于 2023-02-05  发布在  其他
关注(0)|答案(1)|浏览(92)

我尝试为应用程序中的不同角色设置路由,但遇到错误。我想知道我使用的方法是否正确。我想为每个角色指定路由,但我不确定我的方法是否正确。
这是我的web.php文件:

<?php

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

//guest pages
Route::get('/', function () {
  return redirect()->route('login');
});

Auth::routes();

route::middleware('auth')->group(function () {
  Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
  Route::middleware('hasRole:Super Admin')->prefix('SuperAdmin')->group(function () {
    Route::prefix('users')->group(function () {
      //users routes
      Route::get('', [\App\Http\Controllers\UserController::class, 'index'])->name('users.index');
      Route::get('profile/{id}', [\App\Http\Controllers\ProfileController::class, 'show'])->name('profile.show');
      Route::put('profile/{id}', [\App\Http\Controllers\ProfileController::class, 'update'])->name('profile.update');
      Route::delete('/delete_user/{id}', [\App\Http\Controllers\UserController::class, 'destroy'])->name('users.destroy');
      Route::get('ajouter_utilisateur', [App\Http\Controllers\UserController::class, 'create'])->name('user.create');
      Route::post('ajouter_utilisateur', [App\Http\Controllers\UserController::class, 'store'])->name('user.store');
    });
    Route::prefix('fournisseurs')->name('fournisseur.')->group(function () {
      //fournisseurs routes
      Route::get('', [App\Http\Controllers\FournisseurController::class, 'index'])->name('index');
      Route::delete('{id}', [App\Http\Controllers\FournisseurController::class, 'destroy'])->name('destroy');
      Route::get('edit_fournisseur/{id}', [\App\Http\Controllers\FournisseurController::class, 'edit'])->name('edit');
      Route::put('fournisseurs/{id}', [\App\Http\Controllers\FournisseurController::class, 'update'])->name('update');
      Route::get('ajouter_fournisseur', [App\Http\Controllers\FournisseurController::class, 'create'])->name('create');
      Route::post('ajouter_fournisseur', [App\Http\Controllers\FournisseurController::class, 'store'])->name('store');
    });
    Route::prefix('factures')->name('facture.')->group(function () {
      //factures routes
      Route::get('', [App\Http\Controllers\FactureController::class, 'index'])->name('index');
      Route::delete('{id}', [App\Http\Controllers\FactureController::class, 'destroy'])->name('destroy');
      Route::get('ajouter_facture', [App\Http\Controllers\FactureController::class, 'create'])->name('create');
      Route::post('ajouter_facture', [App\Http\Controllers\FactureController::class, 'store'])->name('store');
      Route::get('download/{id}', [App\Http\Controllers\FactureController::class, 'downloadFacture'])->name('downloadFacture');
    });
  });

  Route::middleware('hasRole:Admin')->prefix('Admin')->group(function () {
    Route::prefix('users')->group(function () {
      //users routes
      Route::get('', [\App\Http\Controllers\UserController::class, 'index'])->name('users.index');
      Route::get('profile/{id}', [\App\Http\Controllers\ProfileController::class, 'show'])->name('profile.show');
      Route::put('profile/{id}', [\App\Http\Controllers\ProfileController::class, 'update'])->name('profile.update');
      Route::delete('/delete_user/{id}', [\App\Http\Controllers\UserController::class, 'destroy'])->name('users.destroy');
      Route::get('ajouter_utilisateur', [App\Http\Controllers\UserController::class, 'create'])->name('user.create');
      Route::post('ajouter_utilisateur', [App\Http\Controllers\UserController::class, 'store'])->name('user.store');
    });
    Route::prefix('fournisseurs')->name('fournisseur.')->group(function () {
      //fournisseurs routes
      Route::get('', [App\Http\Controllers\FournisseurController::class, 'index'])->name('index');
    });
    Route::prefix('factures')->name('facture.')->group(function () {
      //factures routes
      Route::get('', [App\Http\Controllers\FactureController::class, 'index'])->name('index');
      Route::get('download/{id}', [App\Http\Controllers\FactureController::class, 'downloadFacture'])->name('downloadFacture');
    });
  });
});

使用此解决方案时,我收到错误消息
优化失败(有关详细信息,请参阅输出控制台)
有人能帮我找到这个问题的解决方案或建议我正确的方法吗?

zzlelutf

zzlelutf1#

对于Laravel中的角色管理,我建议您使用“spatie/Laravel-permission”。这是文档链接:https://spatie.be/docs/laravel-permission/v5/introduction安装此软件包后,您必须为每个角色创建中间件,然后在web.php上检查

相关问题