Laravel Routing- Multiple Route

杀马特。学长 韩版系。学妹 提交于 2019-12-13 07:45:38

问题


I'm using laravel to make an API. The following endpoints will be available:

Currently I have:

Route::get('/{appname}/{network}/{device}', function()
{
    return 'Hello World';
});

This services /appname/network/device/ but what I would like is so it can be the following but in one route without the need for additional routing:

Route::get('/{appname}', function()
{
    return 'App Name';
});

Route::get('/{appname}/{network}', function()
{
    return 'App Name / Network';
});

Route::get('/{appname}/{network}/{device}', function()
{
    return 'App Name / Network / Device';
});

I understand that I could use RESTFUL controllers but this would only work (I believe) with names like:

public function getAppname()
{
    //app
}

But if I used:

public function getAppnameNetwork()
    {
        //app-network
    }

it would become:

/appname-network/

Any advice/help would be greatly appreciated.

Thanks :)


回答1:


I think routing groups may be what you want.

http://laravel.com/docs/routing#route-groups will have more information.

If you need variable prefixes, check out: https://github.com/jasonlewis/enhanced-router




回答2:


You can have optional routing parameters:

Route::get('/{appname}/{network?}/{device?}', function($appname, $network = null, $device = null)
{
    return "$appname - $network - $device";
});


来源:https://stackoverflow.com/questions/21865394/laravel-routing-multiple-route

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!