0

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

2 Answers 2

2

You can have optional routing parameters:

Route::get('/{appname}/{network?}/{device?}', function($appname, $network = null, $device = null)
{
    return "$appname - $network - $device";
});
Sign up to request clarification or add additional context in comments.

Comments

-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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.