New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Proposal] Make route() accept resourceful controller actions #550

Closed
driesvints opened this Issue Mar 12, 2013 · 4 comments

Comments

Projects
None yet
3 participants
@driesvints
Contributor

driesvints commented Mar 12, 2013

Now that I'm using namespaces to separate controllers into different folders for the site (a root folder with Controllers for the frontend and a subfolder admin for controllers for the backend) it comes to me that it's a bit of a hassle to type the namespace every time for the action() function to generate a url for a specific resourceful controller action.

Currently doing this:

Route::resource('users', 'Controllers\Admin\User');
action('Controllers\Admin\User@create');
action('Controllers\Admin\User@edit', $id);

I'm not 100% sure but I don't think you can use route() since that's for named routes only and doesn't seems to be logic to use with resourceful routes since they map to different Controller functions.

Wouldn't it be easier if we could add a name to a resourceful route and define the action in route()? Like this:

Route::resource('users', 'Controllers\Admin\User', array('as' => 'users'));
route('users@create');
route('users@edit', $id);

With users being the named resourceful route of course and attaching the resourceful controller action directly to it. This is especially helpful for controllers which have long namespaces.

I know it's a bit of a combo of route() and action() so maybe a new function resource() for named resources could be created for this?

Route::resource('users', 'Controllers\Admin\User', array('as' => 'users'));
resource('users@create');
resource('users@edit', $id);

It doesn't has to be too difficult to implement. You only need to strip the name from the action, look it up in the routes, get the controller and return a new action route.

@driesvints

This comment has been minimized.

Show comment
Hide comment
@driesvints

driesvints Mar 12, 2013

Contributor

You could add an alias for the namespace to your app config but I'm aiming at named resourceful routes.

Contributor

driesvints commented Mar 12, 2013

You could add an alias for the namespace to your app config but I'm aiming at named resourceful routes.

@taylorotwell

This comment has been minimized.

Show comment
Hide comment
@taylorotwell

taylorotwell Mar 12, 2013

Member

This is already supported. You can do route('users.create'), etc.

Member

taylorotwell commented Mar 12, 2013

This is already supported. You can do route('users.create'), etc.

@JoostK

This comment has been minimized.

Show comment
Hide comment
@JoostK

JoostK Mar 12, 2013

Contributor

Also, if you don't use resourceful routes and still want to dismiss the whole namespace, in your BaseController you could add something like:

public static function at($action)
{
    return get_called_class() . "@{$action}";
}

You could then import the class with the PHP use keyword, to use it like this:

use Admin\Controllers\UserController;

Route::get('users', UserController::at('index'));

This also gives you error checking because you'll get an error if the class can't be found 😃

Contributor

JoostK commented Mar 12, 2013

Also, if you don't use resourceful routes and still want to dismiss the whole namespace, in your BaseController you could add something like:

public static function at($action)
{
    return get_called_class() . "@{$action}";
}

You could then import the class with the PHP use keyword, to use it like this:

use Admin\Controllers\UserController;

Route::get('users', UserController::at('index'));

This also gives you error checking because you'll get an error if the class can't be found 😃

@driesvints

This comment has been minimized.

Show comment
Hide comment
@driesvints

driesvints Mar 12, 2013

Contributor

Thanks for the cool suggestions. I'm learning more and more every day about L4 :)

On 12 Mar 2013, at 21:13, Joost Koehoorn notifications@github.com wrote:

Also, if you don't use resourceful routes and still want to dismiss the whole namespace, in your BaseController you could add something like:

public static function at($action)
{
return get_called_class() . "@{$action}";
}
You could then import the class with the PHP use keyword, to use it like this:

use Admin\Controllers\UserController;

Route::get('users', UserController::at('index'));
This also gives you error checking because you'll get an error if the class can't be found


Reply to this email directly or view it on GitHub.

Contributor

driesvints commented Mar 12, 2013

Thanks for the cool suggestions. I'm learning more and more every day about L4 :)

On 12 Mar 2013, at 21:13, Joost Koehoorn notifications@github.com wrote:

Also, if you don't use resourceful routes and still want to dismiss the whole namespace, in your BaseController you could add something like:

public static function at($action)
{
return get_called_class() . "@{$action}";
}
You could then import the class with the PHP use keyword, to use it like this:

use Admin\Controllers\UserController;

Route::get('users', UserController::at('index'));
This also gives you error checking because you'll get an error if the class can't be found


Reply to this email directly or view it on GitHub.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment