My routes return 404, how can I fix them?

I was just starting to learn the Laravel framework, and I had a problem with routing.

The only route that works is the default home route, which comes with Laravel out of the box.

I use WAMP on Windows, and it uses PHP 5.4.3 and Apache 2.2.22, and also included mod_rewrite and removed 'index.php' from the application.php configuration file to leave an empty line.

I 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 created a view file in the application / views / user / called index.php with some basic HTML code, and in routes.php I 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 404 Not Found mistake. Why is this happening?

+63
php laravel laravel-3 laravel-routing routing
Aug 03 2018-12-12T00:
source share
18 answers

Have you tried adding this to your routes file instead of Route::get('user', "user@index") ?

A piece of text before @ , user in this case will direct the page to the user controller, and a piece of text after @ , index direct the script to user the public function get_index() .

I see that you are using $restful , in which case you can set Route to Route::any('user', 'user@index') . This will handle both POST and GET instead of writing both of them separately.

+22
Aug 03 '12 at 15:38
source share

In my Ubuntu LAMP installation, I solved this problem with the following 2 changes.

  • Enable mod_rewrite on apache server: sudo a2enmod rewrite .
  • Modify /etc/apache2/apache2.conf by changing the "AllowOverride" directive for the / var / www directory (which is my main document root): AllowOverride All

Then restart the Apache server: service apache2 restart

+79
Jan 19 '15 at 18:43
source share

Using WAMP, click the wamp icon โ†’ apache-> apache modules-> scroll and check rewrite_module Restart LoadModule rewrite_module

Please note: the server application will automatically start for you after enabling "rewrite_module"

+63
Jan 23 '13 at 22:33
source share

You tried to check

 http://localhost/mysite/public/index.php/user 

worked? If so, make sure all folders in your path are not capitalized. I had the same situation, and they helped me convert letters to lowercase.

+29
Dec 26 '12 at 19:30
source share

I had a problem with EasyPHP. It was found that I had to specify AllowOverride All in my <Directory> block in httpd.conf . Without this, Apache sometimes ignores your .htaccess .

Mine ended up looking like this ...

 <Directory "D:/Dev"> Options FollowSymLinks Indexes #### NEXT IS THE CRUCIAL LINE #### AllowOverride All Order deny,allow Allow from 127.0.0.1 Deny from all Require all granted </Directory> 
+6
Dec 21 '13 at 14:27
source share

You can try moving root/public/.htaccess to root/.htaccess and it should work

+6
Dec 22 '16 at 18:17
source share

Routes

Use them to identify specific routes that are not controlled by controllers.

Controllers

Use them if you want to use the traditional MVC architecture

Solution to your problem

You do not register controllers as routes unless you want to use a specific "named" route for controller action.

Instead of creating a route for the actions of your controllers, simply register your controller:

 Route::controller('user'); 

Now your controller is registered, you can go to http://localhost/mysite/public/user , and your get_index will be launched.

You can also register all controllers in one go:

 Route::controller(Controller::detect()); 
+5
Aug 03 2018-12-12T00:
source share

Do not forget " RewriteBase " in your public/.htaccess :

For example:

 Options +FollowSymLinks RewriteEngine On RewriteBase /your/folder/public 
+3
May 31 '13 at 23:25
source share

Well, therefore, having patted my head a little over this problem for a little more than a day ... I got up and did what I MUST do yesterday, and FORGOTTEN what is happening!

What Laravel TRYING does here, inserts the index.php file right in front of the track indicated as Route. For example, if you specified Route::get('/account/create', ..., and run your application from say localhost/laravel/authenticate/public/account/create in your browser, then laravel wants to execute localhost/authenticate/public/index.php/account/create , but for this .... Apache should see these requests through /wamp/www/laravel/laravel/authentication/public (your path may vary slightly, depending on where your application is laravel is actually installed, but the final public is the place where the replacement should be performed) "RewriteRule" must be applied.

Fortunately, laravel provides the correct Rewrite rule in a convenient .htaccess file right in the public application folder. PROBLEM, the code in this .htaccess file will not work with the way WAMP is configured out of the box. The reason for this SEEMS is the problem suggested by muvera at the top of this thread - the rewrite_module code must be loaded by Apache before the RewriteRule stuff will work. Heck, that makes sense.

The part that DOES NOT make sense: just stopping and restarting Apache services will not pick up the changes necessary for WAMP to do the right thing with the RewriteRule - I know I tried this many times!

What works: make the changes suggested by muvera (see top of thread) to load the correct modules. Then, reset your entire Windows session, thus completely removing Apache from memory. Restart (reload) WAMP and VOILA! the fix works, the correct RewriteRule, yada, yada is applied; I live happily ever after.

The good news is all of this: now I know more about the .htaccess , RewriteRule and httpd.conf files. There is a good (effective) argument for moving the logic from your application to the public .htaccess file and pasting it into the Directory ... section Directory ... your httpd.conf in the Apache BTW bin folder (especially if you have access to this folder).

+3
Mar 15 '14 at 18:31
source share

Try to include short php tags in php.ini. WAMP usually disables them, and laravel needs them.

0
Sep 30
source share
 Route::get('/', function() { return View::make('home.index'); }); Route::get('user', function() { return View::make('user.index'); }); 

change the value above to

 Route::get('user', function() { return View::make('user.index'); }); Route::get('/', function() { return View::make('home.index'); }); 

You should use '/' (home / default) at the end of your routes

0
Dec 27 '13 at 10:56
source share

you must use the laravel 5 command

  class User_Controller extends Controller { public $restful = true; public function get_index(){ return View('user.index'); } } 

and in routes.php

  Route::get('/', function() { return view('home.index'); }); Route::get('user', function() { return view('user.index'); }); 

For changes to the Laravel 5 command for viewing and the controller, see the documentation in which I had the same error before

0
Mar 29 '15 at 17:43
source share

Just run in your terminal.

  composer dump-autoload 
0
Sep 02 '16 at 11:50
source share

The main problem the route works with is the mod_rewrite.so module on macos, linux is not included in the apache configuration httpad.conf file, so .htaccess can work. I solved this by uncommenting the line:

LoadModule rewrite_module libexec / apache2 / mod_rewrite.so

Remove # from the httpdf.conf line above. Then it will work. enjoy it!

0
Aug 01 '17 at 9:15 on
source share

I think you deleted the default .htaccess file in the laravel public folder. upload the file, it should fix your problem.

0
Aug 28 '17 at 18:12
source share

simple commands with automatic dependency loading

 composer dump-autoload 

and still get your important files missing, go here to see the whole procedure

https://codingexpertise.blogspot.com/2018/11/laravel-new.html

0
Dec 01 '18 at 7:30
source share

If you use Vagrant through Homestead, there may be an error mounting the shared folder. It seems that Vagrant takes your files from this folder and uploads the files that are actually located on the host machine at boot, so if an error occurs, you are essentially trying to access your Laravel installation from the moment you they did it for the first time (thatโ€™s why you only get a โ€œhouseโ€ - it was generated during installation).

You can easily verify this by going into your vm and checking the route / web.php file to see if this is really your file. If this is not the case, exit and do vagrant halt , vagrant up and look for boot errors.

0
Dec 17 '18 at 23:46
source share

Put your code at the top of the route file, it will execute your controller

0
Jan 22 '19 at 10:40
source share



All Articles