[php] My Routes are Returning a 404, How can I Fix Them?

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?

This question is related to php laravel routing laravel-routing laravel-3

The answer is


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"


Just Run in your terminal.

 composer dump-autoload

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

  1. Enable mod_rewrite on the apache server: sudo a2enmod rewrite.
  2. Edit /etc/apache2/apache2.conf, 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


the simple Commands with automatic loads the dependencies

composer dump-autoload

and still getting that your some important files are missing so go here to see whole procedure

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


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

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

change above to

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

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

You have to use '/'(home/default) at the end in your routes


you must be using Laravel 5 the 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');
  });

Laravel 5 command changes for view and controller see the documentation i was having same error before


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


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:

Route::controller('user');

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

You can also register all controllers in one go:

Route::controller(Controller::detect());

If you're using Vagrant though Homestead, it's possible there was an error mounting the shared folder. It looks like Vagrant takes your files from that folder and swaps out the files that are actually on the host machine on boot, so if there was an error, you're essentially trying to access your Laravel installation from when you first made it (which is why you're only getting "home"- that was generated during installation).

You can easily check this by sshing into your vm and checking the routes/web.php file to see if it's actually your file. If it isn't, exit out and vagrant halt, vagrant up, and look for errors on boot.


I was getting the same problem using EasyPHP. 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>

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 a Route::get('/account/create', ..., and execute your app from say localhost/laravel/authenticate/public/account/create on your browser, then laravel wants to execute localhost/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 trailing public 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's public 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 the RewriteRule stuff will work. Heck this makes sense.

The part that DOESN'T make sense: simply stopping and restarting 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, and httpd.conf files now. There is a good (performance) argument for moving the logic from your app's public .htaccess file, and putting it into a Directory ... section of your httpd.conf in your Apache 'bin' folder BTW (especially if you have access to that folder).


Don't forget the "RewriteBase" in your public/.htaccess :

For example :

Options +FollowSymLinks
RewriteEngine On
RewriteBase /your/folder/public

  1. setup .env file
  2. configure index.html
  3. make sure u have .htaccess
  4. sudo service apache2 restart

most probably it's due to cache problems


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 of httpdf.conf. Then it will works.
enjoy!


Have you tried to check if

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

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.


Try enabling short php tags in your php.ini. WAMP has them off usually and laravel needs them on.


You could try to move root/public/.htaccess to root/.htaccess and it should work


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to laravel

Parameter binding on left joins with array in Laravel Query Builder Laravel 4 with Sentry 2 add user to a group on Registration Target class controller does not exist - Laravel 8 Visual Studio Code PHP Intelephense Keep Showing Not Necessary Error The POST method is not supported for this route. Supported methods: GET, HEAD. Laravel How to fix 'Unchecked runtime.lastError: The message port closed before a response was received' chrome issue? Post request in Laravel - Error - 419 Sorry, your session/ 419 your page has expired Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required How can I run specific migration in laravel Laravel 5 show ErrorException file_put_contents failed to open stream: No such file or directory

Examples related to routing

Send data through routing paths in Angular Angular2 Routing with Hashtag to page anchor Passive Link in Angular 2 - <a href=""> equivalent laravel throwing MethodNotAllowedHttpException How to use absolute path in twig functions S3 Static Website Hosting Route All Paths to Index.html AngularJS - How can I do a redirect with a full page load? Web API Routing - api/{controller}/{action}/{id} "dysfunctions" api/{controller}/{id} The requested resource does not support HTTP method 'GET' My Routes are Returning a 404, How can I Fix Them?

Examples related to laravel-routing

Laravel 5.2 Missing required parameters for [Route: user.profile] [URI: user/{nickname}/profile] Laravel: How to Get Current Route Name? (v5 ... v7) Set session variable in laravel Laravel 5 - redirect to HTTPS How Can I Remove “public/index.php” in the URL Generated Laravel? Download files in laravel using Response::download Laravel Controller Subfolder routing My Routes are Returning a 404, How can I Fix Them?

Examples related to laravel-3

Convert laravel object to array Laravel - Pass more than one variable to view My Routes are Returning a 404, How can I Fix Them?