【问题标题】:Getting an error in passing a variable through routes to controller in laravel通过路由将变量传递给laravel中的控制器时出错
【发布时间】:2015-04-01 14:03:08
【问题描述】:

在 app\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'的确切用法

【问题讨论】:

  • 你在使用 Laravel 3 吗?
  • 我使用的是 laravel 4.2
  • 你确定吗?在 Laravel 4.2 中,您的路线如下所示:Route::get('author/{id}', ...

标签: php mysql laravel laravel-routing


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    路线应该是:

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

    【讨论】:

    • 你能解释一下上面代码中'as'=>'author'的确切用法
    • 'as'=>'author' 允许你给路由命名。通常资源路由具有点符号名称,这将允许您指定要访问的命名路由。 laravel.com/docs/4.2/routing#named-routes
    猜你喜欢
    • 2015-01-06
    • 2017-08-09
    • 1970-01-01
    • 2017-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-30
    • 2015-11-20
    相关资源
    最近更新 更多