【问题标题】:Laravel 4 - Routes not resetting to rootLaravel 4 - 路由未重置为root
【发布时间】:2015-08-11 09:56:11
【问题描述】:

我遇到了一个问题,当我去一个带有 href 的路线时,例如

example.com/user/foo

然后单击带有 href 的链接,例如

example.com/cart/bar

URL 设置为

example.com/user/cart/bar

我得到一个错误。问题是 URL 没有重置为根目录,而是将子目录('user')保留在 URL 中。

这是一个用户路径的链接示例:

<li><a href="/user/{{ Auth::user()->username }}">{{ Auth::user()->firstName }} {{ Auth::user()->lastName }}</a></li>

还有路线:

Route::get('/user/{username}', array(
    'before' => 'auth',
    'as' => '/user/{username}',
    'uses' => 'ProfileController@user'
    ));

对视图的调用结果:

return View::make('profile.user')
            ->with('user', $user);

此时,网址为:

example.com/user/john_smith

但是,假设我想查看我的购物车,它的 href 为:

<li><a href="store/cart">Cart</a></li>

还有路线:

Route::get('store/cart', array(
    'as' => 'get-cart',
    'uses' => 'StoreController@getCart'
    ));

对视图的调用结果:

return View::make('store.cart')->with('products', Cart::contents());

网址应该是:

example.com/store/cart

其实是

example.com/user/store/cart

我得到一个“NotFoundHttpException”

【问题讨论】:

    标签: url laravel routes


    【解决方案1】:

    你可以使用url()辅助函数来生成一个绝对url,例如:

    <li><a href="{{ url('store/cart') }}">Cart</a></li>
    

    Check the helper functions.

    【讨论】:

      猜你喜欢
      • 2013-09-19
      • 2015-07-30
      • 2014-06-25
      • 2013-01-15
      • 2015-02-09
      • 2013-05-22
      • 2013-05-27
      • 2013-05-10
      • 1970-01-01
      相关资源
      最近更新 更多