gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 12:26:10 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com