I've just started learning the Laravel framework and I'm having an issue with routing.
The only route that's working is the default home route that's attached to Laravel out of the box.
I'm using WAMP on Windows and it uses PHP 5.4.3, and Apache 2.2.22, and I also have mod_rewrite enabled, and have removed the 'index.php' from the application.php config file to leave an empty string.
I've created a new controller called User:
class User_Controller extends Base_Controller {
public $restful = true;
public function get_index()
{
return View::make('user.index');
}
}
I've created a view file in application/views/user/ called index.php with some basic HTML code, and in routes.php I've added the following:
Route::get('/', function () {
return View::make('home.index');
});
Route::get('user', function () {
return View::make('user.index');
});
The first route works fine when visiting the root (http://localhost/mysite/public
) in my web browser, but when I try to go to my second route with http://localhost/mysite/public/user
I get a 404 Not Found error. Why would this be happening?
Just Run in your terminal.
Try enabling short php tags in your php.ini. WAMP has them off usually and laravel needs them on.
Have you tried to check if
was working? If so then make sure all your path's folders don't have any uppercase letters. I had the same situation and converting letters to lower case helped.
Routes
Use them to define specific routes that aren't managed by controllers.
Controllers
Use them when you want to use traditional MVC architecture
Solution to your problem
You don't register controllers as routes unless you want a specific 'named' route for a controller action.
Rather than create a route for your controllers actions, just register your controller:
Now your controller is registered, you can navigate to
http://localhost/mysite/public/user
and yourget_index
will be run.You can also register all controllers in one go:
I was getting the same problem using EasyPHP. Found that I had to specify
AllowOverride All
in my<Directory>
block inhttpd.conf
. Without this, Apache sometimes ignores your.htaccess
.Mine ended up looking like this...
Have you tried adding this to your routes file instead
Route::get('user', "user@index")
?The piece of text before the
@
,user
in this case, will direct the page to the user controller and the piece of text after the@
,index
, will direct the script to theuser
functionpublic function get_index()
.I see you're using
$restful
, in which case you could set yourRoute
toRoute::any('user', 'user@index')
. This will handle bothPOST
andGET
, instead of writing them both out separately.