laravel报错,NotFoundHttpException in RouteCollection.php line 161:

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了laravel报错,NotFoundHttpException in RouteCollection.php line 161:相关的知识,希望对你有一定的参考价值。

详情见图片

参考技术A

解决方法:

方法 1.在form表单中添加如下的隐藏域代码。

    <input type="hidden" name="_token" value=" csrf_token() " />

方法 2.在form表单中添加代码。 

    csrf_field!! csrf_field() !!

方法 3.注释 Kernel.php 代码

打开 app\\Http\\Kernel.php,在文件中注释掉下面的代码。

    \\App\\Http\\Middleware\\VerifyCsrfToken::class

方法 4. 修改handle()方法

打开 \\app\\Http\\Middleware\\VerifyCsrfToken.php,添加或修改 handle()方法如下:

    public function handle($request, \\Closure $next) // 使用CSRF //return parent::handle($request, $next); // 禁用CSRF return $next($request);

参考技术B 检查下你的路由配置,你请求的这个地址,在路由配置中找不到,所以抛出了异常

Laravel 没有运行我的路线

【中文标题】Laravel 没有运行我的路线【英文标题】:Laravel doesn't running my route 【发布时间】:2016-12-05 03:06:44 【问题描述】:

我是 Laravel 5 的初学者。我创建了一个模型(Cars)、一个控制器(CarController)和一个视图(show.blade.php)。但是,每次我尝试执行我的项目时,都会遇到同样的错误:

Sorry, the page you are looking for could not be found.

1/1
NotFoundHttpException in RouteCollection.php line 161:
in RouteCollection.php line 161
at RouteCollection->match(object(Request)) in Router.php line 821
at Router->findRoute(object(Request)) in Router.php line 691
at Router->dispatchToRoute(object(Request)) in Router.php line 675
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\closure(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\closure(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\closure(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\closure(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 54
at require_once('C:\xampp\htdocs\laravel\public\index.php') in server.php line 21

这些是我的代码:

Car.php(型号)

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Car extends Model

    //

2016_07_30_135543_create_cars_table.php

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCarsTable extends Migration

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    
        Schema::create('cars', function (Blueprint $table) 
            $table->increments('id');
            $table->string('make');
            $table->string('model');
            $table->date('produced_on');
            $table->timestamps();
        );
    

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    
        Schema::drop('cars');
    

CarController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use App\Car;

class CarController extends Controller

    //
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    
        //
    

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    
        //
    

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    
        //
    

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    
        //
        $car = Car::find($id);
        return view('cars.show', array('car' => $car));
    

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    
        //
    

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id)
    
        //
    

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    
        //
    

show.blade.php

<!DOCTYPE html>
<html>
  <head>
    <title>Car  $car->id </title>
  </head>
  <body>
    <h1>Car  $car->id </h1>
    <ul>
      <li>Make:  $car->make </li>
      <li>Model:  $car->model </li>
      <li>Produced on:  $car->produced_on </li>
    </ul>
  </body>
</html>

routes.php

<?php


Route::get('/', function () 
    return view('welcome');
);

Route::resource('cars', 'CarController');

我试试:localhost:8000/laravel/public/cars 本地主机:8000/laravel/public/cars.show localhost:8000/laravel/public/show

我不知道为什么会这样。谁能帮帮我?

【问题讨论】:

localhost:8000/cars:D 我想你已经把整个项目都贴在这里了哈哈哈! 你通过php artisan serve运行服务器? 是的,尝试在您的index() 中返回一些内容,例如:return "Hello Cássia"; 如果您想访问show() 方法,请尝试:localhost:8000/cars/1 其中 1 是您的汽车 ID! 【参考方案1】:

尝试以下方法之一:

http://localhost/laravel/public/cars

或首先通过artisan 命令为您的应用程序提供服务

php artisan serve

这样浏览

http://localhost:8000/cars

【讨论】:

以上是关于laravel报错,NotFoundHttpException in RouteCollection.php line 161:的主要内容,如果未能解决你的问题,请参考以下文章

laravel上传图片报错

laravel 报错 class not exist

Laravel 外键约束报错

Laravel 5.1 报错:[AppHttpRequestsRequest] is not instantiable

laravel使用redis报错

laravel报错MethodNotAllowedHttpException