【问题标题】:Named route not working with url命名路由不适用于 url
【发布时间】:2015-01-28 11:20:57
【问题描述】:

我有一条完美的路线

Route::get('downloadsPage', function()
{
$downloads = Download::get();   
return View::make('index', array('downloads' => $downloads));
});
Route::get('/buy/{id}', function($id)
{
$download = Download::find($id);    
return View::make('buy', array('download' => $download));
});

有一个

return View::make('downloadsPage');

位于我的控制器中。

public function afterSignin() {
    if (Auth::attempt(array('email'=>Input::get('email'), 'password'=>Input::get('password')))) {
        return Redirect::intended('downloadsPage')->with('message', 'Thanks for signing in');
    }

问题是我还有一个导航视图文件,用于同一路线的链接

<li><a href="{{ URL::route('downloadsPage')}}">Store</a></li>

我得到了错误 http://postimg.org/image/bhxezsh3n/

错误来自我认为在视图中未正确设置路线。

我在主视图中还有一个 foreach 循环,它使用路由中传递的参数。

@foreach ($downloads as $download)
<tr>
    <td>{{ $download->name }}</td>
    <td>&pound;{{ round($download->price/100) }}</td>
    <td><a href="/buy/{{ $download->id }}" class="btn btn-primary">Buy</a></td>
</tr>
@endforeach

我正在使用命名路由,但显然遗漏了一些可能很简单的修复方法。

我尝试重写路由并使用 get 和 post 以及传递参数添加相应的控制器功能,但没有奏效。

【问题讨论】:

    标签: laravel laravel-4 laravel-routing


    【解决方案1】:

    这不是命名路由,命名路由应该是:

    Route::get('downloadsPage', ['as' => 'downloadsPage', function()
    {
        $downloads = Download::get();   
        return View::make('index', array('downloads' => $downloads));
    }]);
    

    注意'as' =&gt; 'route.name',它定义了路由的名称。

    the docs阅读更多内容。

    【讨论】:

      猜你喜欢
      • 2016-07-06
      • 2013-09-20
      • 2019-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多