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?
I think you have deleted default .htaccess file inside the laravel public folder. upload the file it should fix your problem.
OK, so after bashing my head on this problem for a little over a day... I got up and did what I SHOULD have done yesterday, and DEBUGGED what was going on!
What Laravel is TRYING to do here, is insert the file
index.php
right in front of the path given as a Route. SO for instance, if you specified aRoute::get('/account/create', ...,
and execute your app from saylocalhost/laravel/authenticate/public/account/create
on your browser, then laravel wants to executelocalhost/authenticate/public/index.php/account/create
, but to do that.... Apache needs to see that requests through/wamp/www/laravel/laravel/authentication/public
(your path may vary somewhat, depending on where your laravel app is actually installed, but the trailingpublic
is where the substitution needs to take place) must have a 'RewriteRule' applied.Thankfully, laravel supplies the correct Rewrite rule in a handy
.htaccess
file right there in your app'spublic
folder. The PROBLEM is, the code in that '.htaccess' file won't work with the way WAMP is configured out of the box. The reason for this SEEMS to be the problem suggested by muvera at the top of this thread -- the rewrite_module code needs to be loaded by Apache before theRewriteRule
stuff will work. Heck this makes sense.The part that DOESN'T make sense: simply
stopping
andrestarting
Apache services will not pick up the changes necessary for WAMP to do the right thing with your RewriteRule -- I know, I tried this many times!What DOES work: make the changes suggested by muvera (see top of thread) to load the correct modules. Then, reset your whole Windows session, thus dumping Apache out of memory altogether. Restart (reload) WAMP, and VOILA! the fix works, the correct RewriteRule is applied, yada, yada; I'm living happily ever after.
The good news out of all this: I know a LOT more about
.htaccess
,RewriteRule
, andhttpd.conf
files now. There is a good (performance) argument for moving the logic from your app'spublic
.htaccess
file, and putting it into aDirectory ...
section of your httpd.conf in your Apache 'bin' folder BTW (especially if you have access to that folder).The Main problem of route not working is there is mod_rewrite.so module in macos, linux not enabled in httpd.conf file of apache configuration, so can .htaccess to work. i have solved this by uncomment the line :
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
Remove the # from above line httpdf.conf. Then it will works. enjoy!
On my Ubuntu LAMP installation, I solved this problem with the following 2 changes.
sudo a2enmod rewrite
.AllowOverride All
Then restart the Apache server:
service apache2 restart
Using WAMP click on wamp icon ->apache->apache modules->scroll and check rewrite_module Restart a LoadModule rewrite_module
Note, the server application restarts automatically for you once you enable "rewrite_module"
you must be using Laravel 5 the command
and in routes.php
Laravel 5 command changes for view and controller see the documentation i was having same error before