【问题标题】:Laravel 5 Routing Object Not Found未找到 Laravel 5 路由对象
【发布时间】:2015-06-26 19:06:39
【问题描述】:

我通常使用 Laravel 4,现在我正在尝试学习 Laravel 5

命名控制器路由有问题:

我的路线如下:

Route::get('/', [
    'uses' => 'HomeController@viewHome', 
    'as' => 'home'
]);

Route::get('/events', [
        'uses' => 'EventController@viewEvent', 
        'as' => 'event'
    ]);

当我将路由作为“家”(localhost/laravel/)运行时,它的工作完美

但是当我将路由作为“事件”(localhost/laravel/events)运行时:找不到对象!

我已经通过像这样交换它来确保 viewEvent 方法正常运行:

Route::get('/', [
    'uses' => 'EventController@viewEvent', 
    'as' => 'home'
]);

Route::get('/events', [
        'uses' => 'HomeController@viewHome', 
        'as' => 'event'
    ]);

我可以运行 viewEvent 但我不能运行 viewHome

我的代码有什么问题吗?

========================= 已解决====================== =======

在@DamienPirzy 的帮助下,我意识到当我禁用 /public/ 文件夹时,我想我也必须让 .htaccess 进入主文件夹:)

感谢大家的快速响应:) 问题已解决

【问题讨论】:

  • (localhost/laravel/event) 但你的路线声明“事件”,复数
  • 只是错字我用 localhost/laravel/events @DamienPirsy 运行它
  • 您是否正确设置了 .htaccess?如果你去laravel/index.php/events可以吗?
  • @DamienPirsy 哇它的工作,但我该如何解决它?
  • @DamienPirsy 我已经解决了,谢谢 :)

标签: php laravel laravel-5 laravel-routing


【解决方案1】:

将此 htaccess 放入公用文件夹。确保你有 apache mod rewrite 工作。

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

【讨论】:

【解决方案2】:

我在 routes.php 中看到了

Route::get('/events', [
        'uses' => 'EventController@viewEvent', 
        'as' => 'event'
    ]);

但是你跑了

localhost/laravel/event  

应该运行

localhost/laravel/events

【讨论】:

  • 只是错字我用 localhost/laravel/events 运行它
  • 尝试添加这个@GandhyOnly Route::get('/events', 'EventController@viewEvent');
【解决方案3】:

你能检查一下 .htaccess 文件吗? 因为屏幕错误来自 Apache。请求没有发送到 Laravel 应用程序。

或检查 mod_rewrite 是否启用?

【讨论】:

  • 酷!我遇到了同样的问题,并试图解决这个问题一个小时,你救了我的命。
【解决方案4】:

尝试将 .htaccess 文件更改为此

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

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

【讨论】:

    【解决方案5】:

    从 public 文件夹中复制 htacess 文件并将其粘贴到根目录中。应该能解决问题,还要检查所有路由的拼写;它们一定是正确的。

    【讨论】:

      【解决方案6】:

      在项目名称后添加 index.php,如 localhost/cms/index.php/ 通过这样做,您的所有路线都可以正常工作

      【讨论】:

        【解决方案7】:

        试试看

        <IfModule mod_rewrite.c>
            <IfModule mod_negotiation.c>
                Options -MultiViews -Indexes
            </IfModule>
        
            RewriteEngine On
        
            # Handle Authorization Header
            RewriteCond %{HTTP:Authorization} .
            RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
        
            # Redirect Trailing Slashes If Not A Folder...
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteCond %{REQUEST_URI} (.+)/$
            RewriteRule ^ %1 [L,R=301]
        
            # Send Requests To Front Controller...
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^ index.php [L]
        </IfModule>
        

        【讨论】:

          猜你喜欢
          • 2015-10-27
          • 2017-10-29
          • 1970-01-01
          • 2016-02-27
          • 2018-12-27
          • 2019-07-13
          • 2016-05-16
          • 2015-05-23
          相关资源
          最近更新 更多