【问题标题】:Routing issue causing Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException error导致 Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException 错误的路由问题
【发布时间】:2014-11-18 23:23:25
【问题描述】:

我在测试我的代码时收到此错误。我知道这是一个路由问题,但我看不出我的路由有什么问题。

以下是导致问题的路线:

Route::get('/messages', 'MessageController@create');
Route::get('/messages/show/{comment}', 'MessageController@show');

这里是控制器:

class MessageController extends BaseController
{

protected $messageForm;

public function __construct(MessageForm $messageForm, MessageRepository $messageRepository,
  MessageRecord $messageRecord)
{
    $this->messageForm = $messageForm;
    $this->messageRepository = $messageRepository;
    $this->messageRecord = $messageRecord;
}

/**
 * Display a listing of the resource.
 * GET /messages
 *
 * @return Response
 */
public function create()
{
    return View::make('message.create');
}



public function show($comment)
{
    $message_id = $this->messageRepository->find($comment);
    return View::make('message.show')->with('comment', $message_id);
}

/**
 * Store a newly created resource in storage.
 * POST /messaages
 *
 * @return Response
 */
public function store()
{
    $data = Input::all() ;
    $this->messageForm->validate($data);

    $messageRecord = new MessageRecord;
    $messageRecord->comment = $data['comment'];

    Return "Comment created";
}
}

composer.json

  {
"name": "Desk",
"description": "Control desk",
"keywords": ["desk"],
"require": {
    "laravel/framework": "4.2.*",
    "ornicar/gravatar-bundle": "1.1.*"
},
"require-dev": {
    "behat/behat": "3.0.*",
    "behat/mink-extension": "~2.0@dev",
    "behat/mink-goutte-driver": "~1.0",
    "phpunit/phpunit": "4.0.*",
    "mockery/mockery": "dev-master",
    "way/generators": "dev-master",
    "doctrine/dbal": "2.3.*"
},
"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/controllers/parts",
        "app/controllers/cross",
        "app/database/migrations",
        "app/database/seeds",
        "app/database/seeds/parts",
        "app/tests/TestCase.php",
        "app/tests/FreshDatabase.php"
    ],
    "psr-4": {
        "Desk\\": "app/desk"
 }
},
"scripts": {
    "post-install-cmd": [
        "php artisan optimize"
    ],
    "post-update-cmd": [
        "php artisan clear-compiled",
        "php artisan optimize"
    ],
    "post-create-project-cmd": [
        "php artisan key:generate"
    ]
},
"config": {
    "preferred-install": "dist"
},
"minimum-stability": "dev",
"prefer-stable": true
}

【问题讨论】:

  • 不要采取错误的方式,与其针对同一个问题发布多个问题,不如坚持一个问题并回复更多信息的请求。很高兴知道哪条路线产生了问题。
  • 目前两条路线都不起作用,这就是我发布它们的原因
  • 你能发布完整的 composer.json 文件吗?我知道您使用的是 S2.4,但您也在使用它之上的东西来处理您的 REST 内容。这不是 FOSRestBundle。
  • 雾散了一点。你知道虽然 Laravel 使用了一些 Symfony 2 组件,但 Laravel 和 Symfony2 框架是完全不同的吗?考虑删除 symfony 2 标签并将其替换为 laravel 标签。我怀疑你可能会得到更多帮助。
  • 您可能需要考虑使用 RESTful 资源控制器...然后您将只有一个路由 Route::resource('message', 'MessageController'),并设置您的控制器方法 as detailed in the docs

标签: php laravel controller routes


【解决方案1】:
  1. 您的show 路由需要$comment 参数。那条路线应该 是:

    Route::get('message/show/{comment}', 'MessageController@show');
    
  2. 您是否在此路由上运行auth 过滤器?如果是这样,请尝试删除 before 过滤器(或暂时更改为['before' => 'none']) 并重新加载路线。

    如果您的AuthController 未设置,或者缺少登录信息 方法,当auth 时,你会得到一个NotFoundHttpException filter.php 中的过滤器尝试重定向到您的登录页面。 (看 类似的问题 here)。

【讨论】:

  • 您的routes.php 文件似乎没有加载;虽然您还没有验证您是否有 any 正在工作的路线,但我假设 none 可以。尝试在终端中运行 php artisan routes 并发布您的输出。很可能是虚拟主机配置问题,see the answer to this question
  • @wadeCunningham 我根据类似的问题编辑了上面的答案:)...看看它是否适用于您的情况。
猜你喜欢
  • 2021-05-28
  • 1970-01-01
  • 2021-04-28
  • 1970-01-01
  • 2020-12-14
  • 2014-06-28
  • 2016-05-21
相关资源
最近更新 更多