在应用程序\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/