【问题标题】:Laravel 4: Add username to every route urlLaravel 4:将用户名添加到每个路由 url
【发布时间】:2014-08-17 03:51:28
【问题描述】:

如何将用户名添加到 url?

示例:www.domain.com/feeds/username

我想将用户名添加到每个路由 url。

::路线::

Route::get('feeds', array('before' => 'auth', 'as' => 'feeds', 'uses' => 'FeedsController@getFeedsPage'));

::控制器::

class FeedsController extends BaseController {
    public function getFeedsPage() {
        return View::make('feeds.index');
    }
}

提前致谢。

迈克尔。

【问题讨论】:

    标签: php laravel laravel-routing


    【解决方案1】:

    你可以试试这个,这样声明路由(通知{username}):

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

    声明class

    class FeedsController extends BaseController {
    
        // Use a parameter to receive the username
        public function getFeedsPage($username) {
            // You may access the username passed via route
            // inside this method using $username variable
            return View::make('feeds.index')->with('username', $username);
        }
    }
    

    现在您可以像这样使用URI

    www.domain.com/feeds/username1
    www.domain.com/feeds/michaelsangma
    

    【讨论】:

    • @WereWolf 成功了!谢谢。视图加载完美,但由于某种原因 CSS 无法正常工作。我上一步更改了 CSS 链接 URL,现在它可以正常工作了。再次感谢。
    猜你喜欢
    • 2014-02-05
    • 2017-05-22
    • 2018-05-24
    • 2015-01-25
    • 2021-02-08
    • 2015-03-07
    • 2013-06-20
    • 2016-08-16
    • 1970-01-01
    相关资源
    最近更新 更多