简体   繁体   中英

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 .

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 ?

i used 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.

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

$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:

+--------+----------+------------------------------+------+---------+------------+
| 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.

If you need to add any more prefixes in the future you just simply add a new Route::prefix("<prefix>")->group($routes); to the routes file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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