Symfony\Component\HttpKernel\Exception\NotFoundHttpException Laravel

2022-08-30 18:20:51

我正在尝试使用RESTful控制器。这是我的:Route.php

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

这是我的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)
    {
            //
    }
}

我的应用程序路由是,它显示“你已到达”消息。但是当我尝试时,它给了我“Symfony\Component\HttpKernel\Exception\NotFoundHttpException”localhost/Test/public/localhost/Test/public/test

这是我的日志:

[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} [] []

我知道这个问题已经被问过很多次了。我经历了许多相关的线程,但只是无法找出解决方案。


答案 1

Laravel中的异常总是意味着它无法找到特定URL路由器。在您的情况下,这可能是您的Web服务器配置,虚拟主机(站点)配置或.htaccess配置中的问题。NotFoundHttpException

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

如您所见,第一行中有一个条件,因此,如果您没有安装或启用mode_rewrite,则所有重写规则都将失败,并且IfModule mod_rewrite.c

localhost/Test/public/

将正常工作,但不是这个:

localhost/Test/public/test

换句话说,这个也应该有效,因为这是它的原始形式:

localhost/Test/public/index.php/test

因为Laravel需要重写它才能工作。

请注意,您不应该使用 /public,您的 URL 应如下所示:

localhost/Test/

这是虚拟主机的文档根目录未正确配置或未指向 /var/www/Test/public 或应用程序所在的任何路径的另一个线索。

所有这些都假设您使用的是Apache 2。


答案 2

我有这个案子。我检查了我所有的代码,我的解决方案是:

php artisan route:cache

因为我忘了清除路由缓存。


推荐