Laravel controller gives not found error

I have installed Laravel 4.2.11 using composer and have created an alias. Currently working on Wamp server.

I've created a controller named UserController.php under "app\controllers" location and edited the routes.php under "app". I have written the following code snippet in routes.php:

Route::resource('User','UserController');

However, when I want to access the url by typing http://localhost/laraveldemo/User, it shows a error message "Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException". Please note that I have not created any view for this yet.

May be I'm missing something as I'm new about Laravel. Seeks your help.

Upvotes: 1

Views: 1277

Answers (2)

Andrew Mack
Andrew Mack

Reputation: 312

Here's one possible reason (and solution)...

For Laravel's URL writer to work you must give the server permissions to rewrite the URL.

You can do this by accessing the Apache Modules list (open up the Wamp server settings and hover over Apache) and turn on the option 'rewrite_module'.

After turning this option on, restart the server. If this was indeed the problem then the URL should work.

Also, just a tip... (and maybe just my preference)... I would try to keep your URL as case-insensitive as possible.

Route::resource('user', 'UserController');

I hope this helps get ya going! Laravel is a fantastic framework!

** EDIT: additional help for the .htaccess file

You also want a .htaccess file in your project root directory.

The contents of the file can include the following (I'm simply copy/pasting a working file for a currently active project)

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

RewriteEngine On

RewriteRule ^(.*)$ public/$1 [L]

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>

Upvotes: 1

Gaurav Dave
Gaurav Dave

Reputation: 7474

Try this:

In your Routes.php

Route::get('/', function()
{
    return View::make('users');
});

URL: localhost/laraveldemo

.htaccess file:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

See, if get users.php file.

Upvotes: 2

Related Questions