简体   繁体   English

分别加载后端和前端路由,互不干扰

[英]Load backend and frontend routes separately without interfering

I have two kind of routes, admin routes and frontend routes.我有两种路由,管理路由和前端路由。

The frontend routes前端路线

Route::get('{locale?}/', ['uses' => '\App\Http\Controllers\loadViewController@home']);
Route::get('{locale?}/{page}', ['uses' => '\App\Http\Controllers\loadViewController@index']);
Route::get('{locale?}/{template?}/{page}', ['uses' => '\App\Http\Controllers\loadViewController@detail']);

The backend routes后端路由

Route::prefix('admin/dashboard')->group(function () {
  Route::get('/', 'DashboardController@index')->name('dashboard'); 
});

Now when i type admin/dashboard or api/admin, laravel uses the frontend routes to load the views, while i want the backend views to be loaded.现在,当我输入 admin/dashboard 或 api/admin 时,laravel 使用前端路由加载视图,而我希望加载后端视图。

So to filter out the backend routes i tried this所以为了过滤掉后端路由,我尝试了这个

Route::group(['where' => ['page' => '^(?!admin|api)$', 'template' => '^(?!admin|api)$']], function ({
  Route::get('{locale?}/', ['uses' => '\App\Http\Controllers\loadViewController@home']);
  Route::get('{locale?}/{page}', ['uses' => '\App\Http\Controllers\loadViewController@index']);
  Route::get('{locale?}/{template?}/{page}', ['uses' => '\App\Http\Controllers\loadViewController@detail']); 
});

which obviously did not work这显然不起作用

Also the frontend routes should not have something like /website , they should all start with /前端路由也不应该有/website之类的东西,它们都应该以/开头

My question is: How can i load the backend and frontend routes separately without interfering when called, even if they have the same url length in terms of parameters, keep in mind that the admin routes always start with /admin or /api.我的问题是:如何在调用时分别加载后端和前端路由而不干扰,即使它们在参数方面具有相同的 url 长度,请记住管理路由始终以 /admin 或 /api 开头。

Note: i can't put the backend routes before the frontend routes注意:我不能将后端路由放在前端路由之前

Thanks in advance!提前致谢!

If you want to you could put a constraint on the locale route parameter:如果您愿意,可以对locale路由参数进行约束:

Route::pattern('locale', '^(?!(api|admin)$)(\w*)');

You can put this in the boot method of you RouteServiceProvider and it will now not allow the locale route parameter to match for 'api' or 'admin' .您可以将它放在RouteServiceProviderboot方法中,它现在将不允许locale路由参数匹配'api''admin'

You can register seperate routes in RouteServiceProvider.您可以在 RouteServiceProvider 中注册单独的路由。 Following is how you can do it.以下是您如何做到这一点。

Inside RouteServiceProvider.php do:在 RouteServiceProvider.php 里面做:

public function map()
{
    $this->mapFrontendRoutes();

    $this->mapAdminRoutes();
}

Definition of mapFrontendRoutes(): mapFrontendRoutes() 的定义:

protected function mapFrontendRoutes()
{
     Route::prefix('{locales?}')
         ->middleware('frontend')
         ->namespace($this->namespace.'\Frontend')
         ->group(base_path('routes/frontend.php'));
}

Definition of mapAdminRoutes(): mapAdminRoutes() 的定义:

protected function mapAdminRoutes()
{
     Route::prefix('admin')
         ->middleware('admin')
         ->namespace($this->namespace.'\Admin')
         ->group(base_path('routes/admin.php'));
}

I personally find this very useful, helps to declare interference free and logical routes.我个人觉得这非常有用,有助于声明无干扰和逻辑路由。 Open to feedback.接受反馈。

The simple way is to group both url's as separate groups.简单的方法是将两个 url 分组为单独的组。 Example as follows:示例如下:

Route::group(['prefix' => 'admin', 'as' => 'admin'], function () {
Route::post('/dashboard', 'AdminController@dashboard');

}); });

Route::group(['prefix' => 'home', 'as' => 'home'], function () {
Route::get('/record/{id}', 'HomeController@getRecord');

}); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM