20
votes

I am trying to use RESTful controller. Here is my Route.php:

Route::resource('test', 'TestController');
Route::get('/', function()
{
    return View::make('hello');
});

Here is my TestController.php

<?php
class TestController extends \BaseController {

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
            return View::make('test.home');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
            //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    {
            //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
            //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
            //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id)
    {
            //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
            //
    }
}

My app route is localhost/Test/public/ and it shows "You have arrived" message. But when I tried localhost/Test/public/test It gives me "Symfony\Component\HttpKernel\Exception\NotFoundHttpException"

Here is my log:

[2014-05-11 14:29:59] production.ERROR: NotFoundHttpException Route: `http://localhost/Test/public/test` [] []
[2014-05-11 14:29:59] production.ERROR: exception 'Symfony\Component\HttpKernel\Exception\NotFoundHttpException' in C:\wamp\www\test\bootstrap\compiled.php:5289
Stack trace:
#0 C:\wamp\www\test\bootstrap\compiled.php(4663): Illuminate\Routing\RouteCollection->match(Object(Illuminate\Http\Request))
#1 C:\wamp\www\test\bootstrap\compiled.php(4651): Illuminate\Routing\Router->findRoute(Object(Illuminate\Http\Request))
#2 C:\wamp\www\test\bootstrap\compiled.php(4643): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#3 C:\wamp\www\test\bootstrap\compiled.php(698): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#4 C:\wamp\www\test\bootstrap\compiled.php(679): Illuminate\Foundation\Application->dispatch(Object(Illuminate\Http\Request))
#5 C:\wamp\www\test\bootstrap\compiled.php(1136): Illuminate\Foundation\Application->handle(Object(Illuminate\Http\Request), 1, true)
#6 C:\wamp\www\test\bootstrap\compiled.php(7218): Illuminate\Http\FrameGuard->handle(Object(Illuminate\Http\Request), 1, true)
#7 C:\wamp\www\test\bootstrap\compiled.php(7815): Illuminate\Session\Middleware->handle(Object(Illuminate\Http\Request), 1, true)
#8 C:\wamp\www\test\bootstrap\compiled.php(7762): Illuminate\Cookie\Queue->handle(Object(Illuminate\Http\Request), 1, true)
#9 C:\wamp\www\test\bootstrap\compiled.php(10768): Illuminate\Cookie\Guard->handle(Object(Illuminate\Http\Request), 1, true)
#10 C:\wamp\www\test\bootstrap\compiled.php(640): Stack\StackedHttpKernel->handle(Object(Illuminate\Http\Request))
#11 C:\wamp\www\test\public\index.php(49): Illuminate\Foundation\Application->run()
#12 {main} [] []

I know this question has been asked many times. I had gone through many relevant threads but just can not figure out the solution.

10
Is your controller namespaced?Max
No it is not but I also tried removing \ infront of BaseController still not working.Harsh

10 Answers

26
votes

A NotFoundHttpException exception in Laravel always means that it was not able to find a router for a particular URL. And in your case this is probably a problem in your web server configuration, virtual host (site) configuratio, or .htaccess configuration.

Your public/.htaccess should look like this:

<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>

As you can see there is a condition in the first line IfModule mod_rewrite.c, so if you don`t have mode_rewrite installed or enabled, all rewrite rules will fail and this

localhost/Test/public/

Will work fine, but not this:

localhost/Test/public/test

In other hand, this one should work too, because this is its raw form:

localhost/Test/public/index.php/test

Because Laravel needs it to be rewritten to work.

And note that you should not be using /public, your URLs should look like this:

localhost/Test/

This is another clue that your virtual host's document root is not properly configured or not pointing to /var/www/Test/public, or whatever path your application is in.

All this assuming you are using Apache 2.

2
votes

Before my web.php was like below

Route::group(['prefix' => 'admin', 'middleware' => ['role:admin']], function() {
        Route::resource('roles','Admin\RoleController');
        Route::resource('users','Admin\UserController');
    });

In url enterd

http://127.0.0.1:8000/admin/roles

and get the same error. The solution was :

Route::group(['middleware' => ['role:admin']], function() {
  Route::resource('roles','Admin\RoleController');
  Route::resource('users','Admin\UserController');
});

Remove 'prefix' => 'admin', as both Controllers are located in Admin folder

1
votes

like Arjan Koole says, I had a similar error

using:

Route::get('/performance/{$submit_type}/{$send_selected}/{$date_a}/{$date_b}', 'PerformanceController@calc');

instead of

Route::get('/performance/{submit_type}/{send_selected}/{date_a}/{date_b}', 'PerformanceController@calc');

So be aware when you use {$stuff}

0
votes

I have found another situation where this error can occur, and that one has nothing todo with rewrites, for once. It's a very nasty typo :)

I had:

Route::get('replicas/item/{id}/{?stub}', ['uses' => 'ProductReplicasController@productview', 'as' => 'database.replicas.productview']);

see the {?stub}, that caused this error with me, it needs to be {stub?} off course. But if you're new to Laravel (like me), that is very easy to miss and could take quite some time to figure out. :)

0
votes

another thing to check is your document root,

mine was:

www/var

and should be:

www/var/mysite/public

Another reason why I hate web development

0
votes

I was getting the same error exception while one project was working but another project with same .htaccess file on same localhost was not working. Turns out since my project directory has capital letter in it, URL also should be capital for routing to work. localhost/minorweb/public/account was not working but localhost/minorWeb/public/account with W capital was working. so, if nothing seems to solve the problem, check your URL is matched with project directory with case.

0
votes

In my scenario, the problem was I've used an extra prefix slash like:

Route::get('/plans', 'PayPalController@getPlansList')->name('paypal.plans');

instead of:

Route::get('plans', 'PayPalController@getPlansList')->name('paypal.plans');
0
votes

Put this in root .htaccess file

The below code do three things :

  1. Remove index.php from url .
  2. Remove public from url .
  3. fix your problem .

Note : RewriteRule ^ index.php [L] this code solve your problem my problem was not added [L] after index.php

RewriteEngine On
<IfModule mod_rewrite.c>
 #Session timeout

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

   Options +FollowSymlinks
   RewriteEngine On

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

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ ^$1 [N]

RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ public/$1

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]


</IfModule>
0
votes

in my case: I had to use POST method but i was using the GET! method in web.php Rout

0
votes

In my case I put {$id} instead {id} in the Route