【问题标题】:pass auth::user as data variable with route使用路由传递 auth::user 作为数据变量
【发布时间】:2016-07-08 13:27:52
【问题描述】:

我希望当用户单击个人资料页面时,我想将 Auth::user()->username 作为参数传递给我的 userController 的 show 方法。我的个人资料链接如下:

<li><a href="{{URL::to('/profile')}}">Profile</a></li>

在我的路线中,我有以下路线

Route::get('/profile/{username}',function(){
     return View::make('user.show')->with($username);
});

我的问题是,当我单击配置文件链接时,如何将我的'/profile/{username}' 中的username 设置为Auth::user()-&gt;username?目前配置文件链接没有附加任何参数

【问题讨论】:

    标签: php laravel url routes


    【解决方案1】:

    一种快速的方法是从 /profile 设置重定向,如果他们想查看其他人的个人资料,它不会破坏功能。

    Route::get('/profile',function(){
        return Redirect::to('/profile/'.Auth::user()->username);
    }
    

    但是,我建议在重定向之前执行 Auth::check()。

    【讨论】:

    • 我没有名为 profile 的页面。我必须将其发送到 user.show 页面
    • 我不确定你的意思。我的代码只会重定向用户并最终使用您在问题中的配置文件/用户名路由。
    【解决方案2】:

    首先 {{URL::to('/profile')}} 没有指向Route::get('/profile/{username}) url,有两条不同的路由

    所以你需要做的是要么改变链接,即

    {{URL::to('/profile/' . \Auth::user()->username)}}
    

    然后在你的路由文件中

    Route::get('/profile/{username}',function($username){
        return View::make('user.show')->with(['username' => $username]);
    });
    

    //注意需要在with()方法中传入数组 或者你可以这样做

    Route::get('/profile/{username}',function($username){
        return View::make('user.show',compact('username'));
    });
    

    【讨论】:

      【解决方案3】:

      当用户点击个人资料链接时:

      <li>
        <a href="{!! route('user.show', Auth::user()->username) !!}">My Profile</a>
      </li>
      

      调用 UserController@show 方法。

      <?php
      
      // routes.php
      
      Route::get('profile/{username}', 'UserController@show')->name('user.show');
      
      // UserController.php
      
      public function show($username)
      {
          $user = User::whereUsername($username)->first();
      
          return view('user.show', compact('user'));
      }
      

      并向用户返回一个 View 响应。

      @更新

      如果你需要的只是将控件重定向到 UserController@show 方法,你可以这样做:

      <li>
        <a href="{!! route('user.profile', Auth::user()->username) !!}">My Profile</a>
      </li>
      
      <?php
      
      // routes.php
      
      Route::get('profile/{username}', function ($username) {
          return redirect()->route('user.show', Auth::id());
      })->name('user.profile');
      

      现在如果你想自定义 UserController@show 动作:

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

      调用 UserController@show 方法。

      <?php
      
      // routes.php
      
      Route::resource('user', 'UserController', ['except' => ['show']);
      Route::get('profile/{username}', 'UserController@profile')->name('user.profile');
      

      现在您可以根据需要删除 UserController@show 方法或将配置文件方法名称更改为显示。

      // UserController.php
      
      public function profile($username)
      {
          $user = User::whereUsername($username)->first();
      
          return view('user.show', compact('user'));
      }
      

      【讨论】:

        【解决方案4】:

        我做了类似以下的事情

        <li><a href="{{URL::to('/profile')}}">Profile</a></li>
        

        在 route.php 中:

        Route::get('/profile',function(){
                return redirect()->route('user.show',[Auth::user()->username]);
        
            });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-02-27
          • 2017-07-06
          • 2023-03-07
          • 1970-01-01
          • 2020-03-06
          • 2016-04-28
          • 1970-01-01
          • 2019-04-06
          相关资源
          最近更新 更多