duannaikuang1301 2014-11-26 05:17
浏览 32
已采纳

Laravel 4路由与params问题

So what I am trying to do is create a link the displays the user profile. The link I desire is localhost/user/{username} but the link I get with with the code I currently have is localhost/user?{username}. Here is my route code:

/* Profile Link */
Route::get('user/{username}', array(
'as' => 'profile-user-link'
));

I am not using a controller. Here is my view code:

<li><a href="{{ URL::route('profile-user-link', Auth::user()->username) }}">My Profile</a></li>

Can someone explain why I am not getting my desired link output. Thank you

  • 写回答

1条回答 默认 最新

  • duanpai1920 2014-11-26 05:29
    关注

    You can use something like this:

    Route::get(
        'user/{username}',
        array(
            'as' => 'profile-user-link',
            'uses' => 'userController@profile' // <-- You didn't use any handler
        )
    );
    

    UserController:

    class UserController extends BaseController {
    
        public function profile($username)
        {
            // Make sure User model and username field exists
            $user = User::where('username', $username)->first();
            // Do something with $user
        }
    }
    

    Alternatively you may also use something like this (Not Good Practice):

    Route::get('user/{username}', function($username) {
        // Make sure User model and username field exists
        $user = User::where('username', $username)->first();
        // Do something with $user
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程