Laravel 没有运行我的路线

Posted

技术标签:

【中文标题】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 没有运行我的路线的主要内容,如果未能解决你的问题,请参考以下文章

Laravel 语言翻译未在第一条路线加载

Laravel 路由没有到达控制器

为啥 Laravel 中的新路线不起作用?

路线在 laravel 4.2 中不起作用

Laravel路线没有采用正确的控制器方法

Laravel 获得路线工作,发布不要