【问题标题】:Laravel 4.2 - NotFoundHttpExceptionLaravel 4.2 - NotFoundHttpException
【发布时间】:2015-01-11 04:52:56
【问题描述】:

我刚开始构建一个 Laravel 应用程序,遇到了路由问题。

  1. Laravel 版本 - 4.2
  2. PHP 版本 - 5.5
  3. 我正在使用 XAMPP
  4. 我在 Apache 上启用了重写模块

这是我的路线文件:

<?php

Route::get('/', array(
    'as' => 'index',
    'uses' => 'IndexController@index'
));

Route::get('/account', array(
    'as' => 'account',
    'uses' => 'AccountController@get_create'
));

我的控制器如下所示:

<?php

class IndexController extends BaseController
{
    public function index()
    {
        return View::make('index');
    }
}

<?php

class AccountController extends BaseController
{
    public function get_create()
    {
        return 'asd';
    }

    public function post_create()
    {

    }
}

这是我的错误:

Symfony\Component\HttpKernel\Exception\NotFoundHttpException thrown with message ""

Stacktrace:
#11 Symfony\Component\HttpKernel\Exception\NotFoundHttpException in C:\xampp\htdocs\Protosite\bootstrap\compiled.php:5680
#10 Illuminate\Routing\RouteCollection:match in C:\xampp\htdocs\Protosite\bootstrap\compiled.php:5004
#9 Illuminate\Routing\Router:findRoute in C:\xampp\htdocs\Protosite\bootstrap\compiled.php:4992
#8 Illuminate\Routing\Router:dispatchToRoute in C:\xampp\htdocs\Protosite\bootstrap\compiled.php:4984
#7 Illuminate\Routing\Router:dispatch in C:\xampp\htdocs\Protosite\bootstrap\compiled.php:715
#6 Illuminate\Foundation\Application:dispatch in C:\xampp\htdocs\Protosite\bootstrap\compiled.php:696
#5 Illuminate\Foundation\Application:handle in C:\xampp\htdocs\Protosite\bootstrap\compiled.php:7744
#4 Illuminate\Session\Middleware:handle in C:\xampp\htdocs\Protosite\bootstrap\compiled.php:8351
#3 Illuminate\Cookie\Queue:handle in C:\xampp\htdocs\Protosite\bootstrap\compiled.php:8298
#2 Illuminate\Cookie\Guard:handle in C:\xampp\htdocs\Protosite\bootstrap\compiled.php:10961
#1 Stack\StackedHttpKernel:handle in C:\xampp\htdocs\Protosite\bootstrap\compiled.php:657
#0 Illuminate\Foundation\Application:run in C:\xampp\htdocs\Protosite\public\index.php:49

编辑:我忘了提一件事,索引工作正常,它是有错误的帐户页面。

我看过类似的问题,但由于某种原因他们没有解决。

干杯!

【问题讨论】:

  • 你应该删除 /account 前面的斜线,这在 Laravel 中是不需要的。

标签: php laravel


【解决方案1】:

听起来你的重写不起作用。如果在/account 之前的 URL 中添加index.php 是否有效?

例如,yourdomain.com/account 将变为 yourdomain.com/index.php/account

如果您的重写不起作用,但您的.htaccess 目录中有.htaccess 文件,那么您可能需要在您的apache 配置中允许覆盖。这是 XAMPP 和 Laravel 的虚拟主机配置示例。

我已经标记了您需要更改的行。将第一个和第三个更改为指向您网站目录中的public 目录。然后将第二行更改为您在网站上使用的域名。

<VirtualHost *:80>
    DocumentRoot "C:/xampp/htdocs/yourdomain/public"      # Change this line
    ServerName yourdomain.com                             # Change this line
    <Directory "C:/xampp/htdocs/yourdomain/public">       # Change this line
        Options Indexes FollowSymLinks ExecCGI Includes
        AllowOverride All    # This line enables .htaccess files
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

您需要重新启动 Apache 才能使这些设置生效。

【讨论】:

  • 感谢您的回复。我已经尝试过了,它可以工作,所以谢谢。如果您能给我任何有关如何使正常 URL 正常工作的指示,那就太好了。再次感谢。
  • 您的public/ 目录中是否有一个名为.htaccess 的文件?如果您没有.htaccess 文件,您可以使用Laravel Docs 中的版本。这是从 URL 中删除 index.php 的文件。要使其正常工作,您需要在 Apache 配置中为您的网站目录设置 AllowOverride All
  • 我使用的是与 Laravel 一起安装的 htaccess,我还在 Laravel 文档上尝试了其他版本,但仍然无法正常工作。但是,我设法使用了php artisan serve 命令,这似乎工作正常。
  • 默认情况下,Apache 会忽略目录中的.htaccess 文件。您需要告诉 Apache 使用 .htaccess 文件。您是否在 apache 中为您的网站设置了虚拟主机?如果是这样,您可以编辑您的问题并将您的虚拟主机配置粘贴到那里吗?
  • 我在回答中添加了 XAMPP 和 Laravel 的示例虚拟主机配置。
猜你喜欢
  • 2015-12-20
  • 2014-10-17
  • 2016-12-02
  • 1970-01-01
  • 1970-01-01
  • 2017-01-23
  • 2013-08-17
  • 2019-03-12
  • 2015-03-03
相关资源
最近更新 更多