Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

michael's avatar

Laravel 4.3 Linking to Routed Controller Methods using HomeController@getIndex

I'm having a terrible time using either URL::action() or action() methods to generate a url to a routed controller action. I'm upgrading my application from L4.2 to L4.3 and I'm looked through all the Illuminate files to try to figure it out and I can not.

It appears as though, when using "action("HomeController@getIndex')" it attempts to find a route named HomeController@getIndex versus \App\Http\Controllers\HomeController@getIndex as it should be.

Any help would be much appreciated.

0 likes
11 replies
jehad's avatar

You have two options here (at least that I'm aware of):

1) Use the best-practice of always naming your routes and instead of action() you'll use route() - this helped me migrate an app to L5 without touching a single line of Blade.

2) Muddy your views by specifying the full namespace - you probably don't want that.

It would be nice to have some sort of automatic namespace resolution to ease the pain of migrating to L5. If there is already the option of doing so, I don't know how. If not, maybe Taylor should look into adding this functionality.

michael's avatar

Wouldn't using option #1 require me to edit all my blade templates? Seeing as I'm currently using:

<a href="{{ action('Controller@method') }}">Link</a>

Also based on the code below I thought it was already automatically resolving controller routes as well without needing the entire namespace, because whoever does the new L5 videos said it was already taken care of, in the first video.

But I am currently using:

Route::controller('member', 'UserController');

So perhaps that is it. I'll have to test out using:

Route::get('member', ['uses' => 'UserController@method']);

Although this wouldn't help it would help narrow down the problem.

Cocoon's avatar

I had the same problem with Laravel 5.

Doing something like this will work

Route::controller('users', 'UserController');

But when I want to access the action, I would have to add the full namespace, which is kind of ugly in the view.

action('Acme\Http\Controllers\UserController@show');

I guess Taylor is working on this...

1 like
michael's avatar

Hmm it also doesn't work if you do get(), post() functions to declare the controller routes.

But, I did just notice that if you were to name the route the same as the uses field.

post('member/login', ['as' => 'UserController@postLogin', 'uses' => 'UserController@postLogin']); 

and it was able to resolve UserController@postLogin using

action('UserController@postLogin');

Obviously this defeats the purpose of using

Route::controller('member', 'UserController');
jehad's avatar

That is quite a nasty but creative workaround you have there :))

Actually route naming would go like this:

Route::get('/', ['as' => 'home', 'uses' => 'DashboardController@home'] );

Then, in your view, you'd be doing this:

<a href="{!! route('home') !!}">Home</a>

I don't have too many named routes declared mainly because I use Route::resource() a lot, and that automatically creates the route names that I can use for reference.

I try to keep as far away as possible from Route::controller(), and only use it for the built-in password reminders functionality - but even then I create a couple of aliases that I can reference in my view:

// Password reminders
Route::get('/password/remind', ['as' => 'passwordReset', 'uses' => 'Auth\RemindersController@getRemind'] );
Route::post('/password/remind', ['as' => 'passwordReset', 'uses' => 'Auth\RemindersController@postRemind'] );
Route::controller('password', 'Auth\RemindersController');
michael's avatar

I know how route naming is supposed to work I was stating if you name the route the same value as the "as" value it will resolve automatically during reference as a URL. Such as

<a href="{!! action('UserController@getLogin') !!}">Login</a>

Personally I've never used the reason to use a Resource controller versus a RESTful Controller as I do not need to specify my routes anywhere at all, I need only make the function and then reference it via

{!! action('UserController@getNewMethod') !!}

or

{!! URL::action('UserController@getNewMethod') !!}

Where as with a Resource controller your limited to the 8, or so methods as far as routes go. At least that's what I've understood.

Upon re-reading perhaps it's because you need to reference routes by name. You can use the following to assign route names use Route::controller()

Route::controller('member', 'UserController', [
        'getLogin' => 'user.login',
        'postLogin' => 'user.login.submit'
]);

I didn't know this and loved it once I figured it out in the code.

jehad's avatar

You can add extra methods to a resource route if you need to - I certainly do, most of the times.

I wasn't saying you don't know how route naming works, rather I was showing what I'm using. If you alter your routes in that way to cater to the views, imagine the nightmare when you need to change anything down the line - either the controller name or the method name.

Upgrading to L5 sure isn't easy, but a lot of the issues I've encountered taught me some good lessons about organizing my code in a better, more maintainable way. Then again, some issues just taught me new curse words :D

michael's avatar

It still appears throughout that the code is supposed to be able to handle referencing a controller via action('Controller@method') because throughout the routing code it adds/checks for/removes namespacing seemingly to handle the new namespacing. I just don't know what too many of these methods do haha.

jehad's avatar

Yes, you don't have to worry about controller namespacing in the rest of your application, but views aren't that clever yet. You'll probably have to refactor both your routes as well as your views. But this will be worth your time in the long run.

michael's avatar

Oh I see. This kinda sucks since I chose to go with RESTful controllers because I could reference the url's in views via the action() method haha now I have to either wait until the Views get smarter as I'm sure they will or change like 200 views because I used action() method's extensively.

michael's avatar

So here's my question. why would a view be any different than anywhere else in the application? I mean if

URL::action('HomeController@getIndex');

Works within Models and such, why would it be any different in a view? Considering that

{!! action('HomeController@getIndex') !!}

would only be processed into

<?php echo action('HomeController@getIndex'); ?>

Meaning it'd be using the same action() function. I could definitely be wrong, just was thinking about this.

Please or to participate in this conversation.