【问题标题】:Controller method not found in Laravel 5.1在 Laravel 5.1 中找不到控制器方法
【发布时间】:2016-03-18 14:57:57
【问题描述】:

我是 Laravel 的新手。我有以下路线:

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

Route::get('about', 'PagesController@about');

这是我的控制器:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class PagesController extends Controller
{
    public function about()
    {
        return 'about';
    }   
}

当我导航到:http://localhost/laravelnew/public/about/ 时,我收到 404 Not found。我不明白为什么。

我在公共文件夹中的 .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>

【问题讨论】:

  • 尝试在您的浏览器中执行此操作localhost/laravelnew/about 您的网址中不应包含 /public/
  • @Franco:那行不通。我刚收到请求的 URL /laravelnew/public/about/ 在此服务器上找不到。
  • 如果你转到localhost/laravelnew/public,你会看到 laravel 欢迎屏幕吗?
  • 我的意思是 /public/ 不应该在你的 url 中你使用的是原始的 '.htaccess' 吗?
  • @amirbar:不,因为我已经删除了那条路线。我正在关注本指南:laracasts.com/series/laravel-5-fundamentals/episodes/4 当我将路线更改为 ('/', 'PagesController@about'') 时,它可以工作。

标签: php laravel


【解决方案1】:

我看到你在本地工作,所以你应该启用 mod_rewrite 让它工作。

干杯!

编辑:

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

    RewriteEngine On
    RewriteBase /

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

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

【讨论】:

  • 我为什么要这样做?
  • mod_rewrite 应该被启用,这样漂亮的 url 才能工作。
  • @Bryan 确定一下,您在启用 mod_rewrite 后确实重新启动了服务器?
  • @Drown:是的,我做到了:)
  • @Bryan 你能用公共文件夹中的 .htaccess 文件更新你的帖子吗?
猜你喜欢
  • 1970-01-01
  • 2023-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-08
  • 2016-04-04
  • 2014-03-09
  • 2014-05-12
相关资源
最近更新 更多