I've got laravel set up on a domain on a linux host and I have a WAMP local host set up.
The only route that works is the root, when ever I try go to another route such as domain.com/account I get a "Controller method not found." error.
In my routes.php file I have:
Route::controller('','LoginController');
Route::controller('account', 'AccountController');
In my LoginController, I have just two methods. getIndex
and postIndex
.
After a couple of hours Googling with no results and playing around with the routes file amongst things, still nothing worked.
I tried adding the below route which didn't work either.
Route::any('hello', function(){
return 'hello!';
});
However, I then commented out my Route::controller('','LoginController');
line and the other routes started working!
I then changed it to Route::controller('login','LoginController');
and this and the other routes still worked. I then changed it to Route::any('','LoginController@getIndex');
and the root and other routes still worked. However, doing it this way, when I cliked the login button on my page nothing happened.
So my question really is, is there something wrong with doing Route::controller('','LoginController');
? As everything else seems to 'work'
Laravel save an internal collection of registered routes in the $routes
member of the Router
class. When dispatching a request, a process of picking each element from this collection and test with current request will be executed to find out which route will be handle. This process is affected by the order of your route registering statements.
When testing each route with the current request, the picked route will be compiled and have a regex pattern. This pattern will be use to check with the current URI by the preg_match
function as you can see at this line in Laravel source.
When using Route::controller
a special route will be add to your routes
collection. If your input is Route::controller($uri, $controller)
then this special routes will have a regex pattern as ^/$uri/?P<_missing>(.*)$
and it tells Laravel that this request belong to a missing method of the $controller
controller class.
In your case, you have set the value of $uri
to an empty string which cause the regex of the special route to be ^/?P<_missing>(.*)$
(setting $uri
with the string /
cause the same effect). Well, this regex will match every URI. So, the internal route looking up process will abort when look to this special route. This is the reason while the exception has been thrown.
You should not use an empty string or the/
string when register with the Route::controller
method like the way you did. Instead, use Route::resource
or explicit calls (Route::get
, Route::post
, ...) to handle your top level routes.
I have not tried that, but maybe you could add a "/":
Route::controller('/','LoginController');
Edit
I was able to reproduce the issue and I solved by changing the order of your route lines:
Route::controller('accounts', 'AccountController');
Route::controller('','LoginController');