【问题标题】:Symfony\Component\HttpKernel\Exception\NotFoundHttpException LaravelSymfony\Component\HttpKernel\Exception\NotFoundHttpException Laravel
【发布时间】:2014-06-28 22:07:37
【问题描述】:

我正在尝试使用 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)
    {
            //
    }
}

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

这是我的日志:

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

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

【问题讨论】:

  • 你的控制器有命名空间吗?
  • 不,不是,但我也尝试删除 BaseController 的 \ infront 仍然无法正常工作。
  • NotFoundHttpException Laravel 中的异常总是意味着它无法找到特定 URL 的路由器

标签: php laravel laravel-4


【解决方案1】:

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

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

如你所见,第一行有一个条件IfModule mod_rewrite.c,所以如果你没有安装或启用mode_rewrite,所有的重写规则都会失败,这个

localhost/Test/public/

可以正常工作,但不是这样:

localhost/Test/public/test

另一方面,这个也应该可以工作,因为这是它的原始形式:

localhost/Test/public/index.php/test

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

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

localhost/Test/

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

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

【讨论】:

  • 嗨安东尼奥。谢谢回复。当我尝试加载 localhost/Test/public/index.php/test 时,.htaccess 似乎有问题它正在加载页面。但我的 .htaccess 与您指定的相同。此外,我在服务器上还有一个项目,它与 url rewrite 一起工作正常。
  • 这个项目也是我几周前制作的一个 laravel 项目,它运行良好。但是当我尝试开始这个新项目时,我开始收到这个错误。
  • 虚拟主机配置是针对每个站点的,因此您的所有站点都可以正常工作,而这个不能,它可能仍然是您的虚拟主机/Apache 配置。 .htaccess 仅使用 Web 服务器和虚拟主机功能,因此它不太可能是问题的根源,因为成千上万的开发人员无需接触即可使用它。
  • 感谢安东尼奥的帮助。我启用了虚拟主机模块并将我的 URL 转换为 test.dev,现在它可以工作了。
  • 那么 3 级别名的正确 htaccess 设置是什么,即 localhost/Test/public/test
【解决方案2】:

我发现了另一种可能发生此错误的情况,并且这一次与重写无关。这是一个非常讨厌的错字:)

我有:

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

请参阅导致我出现此错误的 {?stub},它需要是 {stub?} 偏离路线。但是如果你是 Laravel 的新手(像我一样),那很容易错过,可能需要相当长的时间才能弄清楚。 :)

【讨论】:

    【解决方案3】:

    要检查的另一件事是您的文档根目录,

    我的是:

    www/var

    应该是:

    www/var/mysite/public

    我讨厌 Web 开发的另一个原因

    【讨论】:

      【解决方案4】:

      当一个项目正在运行时,我遇到了相同的错误异常,但在同一 localhost 上具有相同 .htaccess 文件的另一个项目无法运行。事实证明,因为我的项目目录中有大写字母,所以 URL 也应该是路由工作的大写字母。 localhost/minorweb/public/account 不工作,但使用 W 资本的 localhost/minorWeb/public/account 工作正常。 所以,如果问题似乎没有解决,检查你的 URL 是否与项目目录匹配。

      【讨论】:

        【解决方案5】:

        就像 Arjan Koole 所说,我遇到了类似的错误

        使用:

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

        而不是

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

        所以在使用 {$stuff} 时要注意

        【讨论】:

          【解决方案6】:

          之前我的web.php 如下所示

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

          在网址中输入

          http://127.0.0.1:8000/admin/roles

          并得到同样的错误。解决方案是:

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

          删除'prefix' => 'admin',因为两个控制器都位于Admin文件夹中

          【讨论】:

            【解决方案7】:

            在我的场景中,问题是我使用了 额外的前缀斜杠,例如:

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

            代替:

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

            【讨论】:

              【解决方案8】:

              把这个放在根.htaccess文件中

              下面的代码做了三件事:

              1. 从 url 中删除 index.php
              2. 从网址中删除 public
              3. 解决您的问题。

              注意: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>
              

              【讨论】:

                【解决方案9】:

                就我而言: 我必须使用POST 方法,但我在web.php Rout 中使用GET! 方法

                【讨论】:

                  【解决方案10】:

                  在我的例子中,我把 {$id} 放在 Route 中而不是 {id}

                  【讨论】:

                    【解决方案11】:

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

                    php artisan route:cache
                    

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

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 2021-05-28
                      • 1970-01-01
                      • 2014-08-13
                      • 2021-04-28
                      • 1970-01-01
                      • 1970-01-01
                      相关资源
                      最近更新 更多