简体   繁体   English

laravel - 如何在不影响现有路由的情况下在所有 url 中添加前缀

[英]laravel - how to add prefix in all url without affecting route existing

i have url like this mysite.com/company , i want add prefix to url to become mysite.com/home/company .我有这样的网址mysite.com/company ,我想为网址添加前缀以成为mysite.com/home/company

I've tried to add a route group, but that requires me to update all existing routes.我试图添加一个路由组,但这需要我更新所有现有的路由。

can i add a prefix in all url without affecting the existing route ?我可以在所有 url 中添加前缀而不影响现有路由吗?

i used laravel 5.6我使用了 Laravel 5.6

I have created a sandbox so that you can view and play around with the code used for this answer.我创建了一个沙箱,以便您可以查看和使用用于此答案的代码。

I know the sandbox uses a different Laravel version (version 7), but looking at the documentation for version 5.6 the routing does not seem to be that much different than that of version 7.我知道沙箱使用不同的 Laravel 版本(版本 7),但是查看 5.6 版的文档,路由似乎与版本 7 的路由没有太大区别。

What you can do is wrap the already existing routes inside an anonymous function and assign it to a variable, you can then use this variable and pass it as a parameter to the group routing function along with a prefix , eg您可以做的是将已经存在的路由包装在一个匿名函数中并将其分配给一个变量,然后您可以使用此变量并将其作为参数与prefix一起传递给group路由函数,例如

$routes = function() {
    Route::get('company', function () {
        return 'companies';
    });

    Route::get('company/{company}', function ($company) {
        return "company $company";
    });

    Route::delete('company/{company}', function ($company) {
        return "deleting company $company...";
    });

    Route::get('company/{company}/staff', function ($company) {
        return "staff list for company $company...";
    });
};

Route::prefix('/')->group($routes);
Route::prefix('/home')->group($routes);

When running php artisan route:list the following is returned:运行php artisan route:list ,将返回以下内容:

+--------+----------+------------------------------+------+---------+------------+
| Domain | Method   | URI                          | Name | Action  | Middleware |
+--------+----------+------------------------------+------+---------+------------+
|        | GET|HEAD | api/user                     |      | Closure | api        |
|        |          |                              |      |         | auth:api   |
|        | GET|HEAD | company                      |      | Closure | web        |
|        | GET|HEAD | company/{company}            |      | Closure | web        |
|        | DELETE   | company/{company}            |      | Closure | web        |
|        | GET|HEAD | company/{company}/staff      |      | Closure | web        |
|        | GET|HEAD | home/company                 |      | Closure | web        |
|        | GET|HEAD | home/company/{company}       |      | Closure | web        |
|        | DELETE   | home/company/{company}       |      | Closure | web        |
|        | GET|HEAD | home/company/{company}/staff |      | Closure | web        |
+--------+----------+------------------------------+------+---------+------------+

You can see above that the routes can now be accessed via both / and home/ , eg http://example.com/company and http://example.com/home/company without the need for duplicating routes.您可以在上面看到路由现在可以通过/home/访问,例如http://example.com/companyhttp://example.com/home/company无需复制路由。

If you need to add any more prefixes in the future you just simply add a new Route::prefix("<prefix>")->group($routes);如果以后需要添加更多前缀,只需添加一个新的Route::prefix("<prefix>")->group($routes); to the routes file.到路由文件。

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

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