[英]Symfony\Component\HttpKernel\Exception\NotFoundHttpException Laravel
I am trying to use RESTful controller.我正在尝试使用 RESTful 控制器。 Here is my
Route.php
:这是我的
Route.php
:
Route::resource('test', 'TestController');
Route::get('/', function()
{
return View::make('hello');
});
Here is my TestController.php
这是我的
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.我的应用程序路由是
localhost/Test/public/
并显示“您已到达”消息。 But when I tried localhost/Test/public/test
It gives me "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException"但是当我尝试
localhost/Test/public/test
它给了我“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.
我经历了许多相关的线程,但就是想不出解决方案。
A NotFoundHttpException
exception in Laravel always means that it was not able to find a router for a particular URL . Laravel 中的
NotFoundHttpException
异常总是意味着它无法找到特定 URL的路由器。 And in your case this is probably a problem in your web server configuration, virtual host (site) configuratio, or .htaccess configuration.在您的情况下,这可能是您的 Web 服务器配置、虚拟主机(站点)配置或 .htaccess 配置中的问题。
Your public/.htaccess should look like this:您的 public/.htaccess 应如下所示:
<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如您所见,第一行
IfModule mod_rewrite.c
有一个条件,因此如果您没有安装或启用 mode_rewrite,所有重写规则都将失败,这
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.因为 Laravel 需要重写它才能工作。
And note that you should not be using /public, your URLs should look like this:请注意,您不应使用 /public,您的 URL 应如下所示:
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.这是另一个线索,表明您的虚拟主机的文档根目录未正确配置或未指向 /var/www/Test/public,或您的应用程序所在的任何路径。
All this assuming you are using Apache 2.所有这些都假设您使用的是 Apache 2。
Before my web.php
was like below在我的
web.php
像下面这样之前
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
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删除 'prefix' => 'admin',因为两个控制器都位于 Admin 文件夹中
like Arjan Koole says, I had a similar error就像 Arjan Koole 说的,我也有类似的错误
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}所以当你使用 {$stuff} 时要注意
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.看到 {?stub},导致我出现这个错误,它当然需要 {stub?}。 But if you're new to Laravel (like me), that is very easy to miss and could take quite some time to figure out.
但是如果你是 Laravel 的新手(像我一样),这很容易被忽略并且可能需要很长时间才能弄清楚。 :)
:)
another thing to check is your document root,要检查的另一件事是您的文档根目录,
mine was:我的是:
www/var万维网/无功
and should be:并且应该是:
www/var/mysite/public www/var/mysite/public
Another reason why I hate web development我讨厌 Web 开发的另一个原因
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.当一个项目正在运行时,我遇到了相同的错误异常,但在同一本地主机上具有相同 .htaccess 文件的另一个项目不工作。 Turns out since my project directory has capital letter in it, URL also should be capital for routing to work.
事实证明,由于我的项目目录中有大写字母,因此 URL 也应该大写以便路由工作。 localhost/minorweb/public/account was not working but localhost/minorWeb/public/account with W capital was working.
localhost/minorweb/public/account不工作,但localhost/minorWeb/public/account与 W 资本工作。 so, if nothing seems to solve the problem, check your URL is matched with project directory with case.
因此,如果似乎没有任何方法可以解决问题,请检查您的 URL 是否与项目目录匹配。
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');
Put this in root .htaccess
file把它放在根
.htaccess
文件中
The below code do three things :下面的代码做了三件事:
Note : RewriteRule ^ index.php [L]
this code solve your problem my problem was not added [L]
after index.php
注意:
RewriteRule ^ index.php [L]
这段代码解决了你的问题 我的问题没有在index.php
之后添加[L]
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>
in my case: I had to use POST
method but i was using the GET!
就我而言:我必须使用
POST
方法,但我使用的是GET!
method in web.php Rout
web.php Rout
方法web.php Rout
就我而言,我将{$id}
{id}
放在了 Route 中而不是{id}
I had this case.我有这个案子。 I checked all of my codes and my solution was:
我检查了所有代码,我的解决方案是:
php artisan route:cache
Because I forgot to clear the route cache.因为我忘记清除路由缓存了。
I am trying to use RESTful controller.我正在尝试使用RESTful控制器。 Here is my
Route.php
:这是我的
Route.php
:
Route::resource('test', 'TestController');
Route::get('/', function()
{
return View::make('hello');
});
Here is my TestController.php
这是我的
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.我的应用程序路由为
localhost/Test/public/
,它显示“您已经到达”消息。 But when I tried localhost/Test/public/test
It gives me "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException"但是当我尝试
localhost/Test/public/test
它给了我“ 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.
我经历了许多相关的话题,但无法解决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.