本文介绍了路由问题导致Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这是导致问题的路由:

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

/ **
*显示资源的列表。
* GET / messages
*
* @return响应
* /
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);
}

/ **
*将新创建的资源存储在存储中。
* POST / messaages
*
* @return响应
* /
public function store()
{
$ data = :all();
$ this-> messageForm-> validate($ data);

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

返回评论已创建;
}
}

composer.json

  {
name:Desk,
description:Control desk,
keywords [desk],
require:{
laravel / framework:4.2。*,
ornicar / gravatar-bundle:1.1。 $ b},
require-dev:{
behat / behat:3.0。*,
behat / mink-extension:~2.0dev
behat / mink-goutte-driver:〜1.0,
phpunit / phpunit:4.0。*,
mockery / mockery ,
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
}

解决方案

  1. show code> $ comment 参数。该路由应该
    是:

      Route :: get('message / show / {comment}','MessageController @显示'); 


  2. 您是否正在运行 auth 过滤器在这条路线?如果是这样,尝试删除过滤器之前
    (或暂时更改为 ['before'=>'none']
    并重新加载



    如果您的 AuthController 未设置或缺少登录
    方法,当 filter.php 中的 auth
    过滤器将获得 NotFoundHttpException code>尝试重定向到您的登录页面。 (请参阅
    相似问题
    )。



I am getting this error when testing my code. I know it is a routing issue but I can't see anything wrong with my routes.

Here is the routes that are causing the problems:

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

Here is the controller:

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
}
解决方案
  1. Your show route expects a $comment parameter. That route shouldbe:

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

  2. Are you running an auth filter on this route? If so, Try removingthe before filter (or change it temporarily to ['before' => 'none'])and reload the route.

    If your AuthController is not set up, or is missing the loginmethod, you will get a NotFoundHttpException when the authfilter in filter.php tries to redirect to your login page. (Seesimilar questionhere).

这篇关于路由问题导致Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 04:15