php - 在 Laravel 中通过路由将变量传递到 Controller 时出现错误

标签 php mysql laravel laravel-routing

在应用程序\routes.php中 Route::get('author/(:any)',array('as'=>'author','uses'=>'AuthorsController@get_view'));

在app\controllers\AuthorsController.php

<?php
class AuthorsController extends BaseController {
public $restful=true;

 public function get_view($id){
  return View::make('authors.view')
->with('title','Author View Page')
->with('author',Author::find($id));
 }

 }

在views\authors\view.blade.php中

<!DOCTYPE html>
<html>
<head> <title> {{ $title }}</title> </head>
<body>
<h1> {{$author->name}}</h1>
<p> {{ $author->bio}}  </p>
<p><small>{{$author->updated_at}} <small></p>
</body>
</html>

在数据库中有一个名为authors的表,其中包含“id”,“name”,“bio”,“created_at”,“updated_at”列。 另请解释一下上面代码中 'as'=>'authors' 的确切用法

最佳答案

您的路线使用旧式语法。在 4.2 中,在 URI 中使用 {} 来指示变量。

Route::get('author/{id}', array('as' => 'author', 'uses' => 'AuthorsController@get_view'));

关于php - 在 Laravel 中通过路由将变量传递到 Controller 时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28280898/

相关文章:

php - Laravel 在没有竞争条件的情况下在保存之前获取最新的非递增值

php - Laravel 5 找不到 css 文件

PHP - 如何在我的数据库中的 4 个项目之间放置横幅

php - 根据包循环产品

php - htaccess重定向问题

mysql - 具有重复值的列的数据库索引

php - 拉维尔 5 : Enable laravel-debugbar

php - Mysql 通配符和数组替换错误

Mysql Group_Concat函数

php - mysql 在我的表单中插入错误