简体   繁体   English

Laravel:如何将$ id设置为Auth :: user()-> id; 在资源控制器方法中

[英]Laravel: How to set $id to Auth::user()->id; in resource controller methods

I have Laravel's built in auth working. 我有Laravel内置的身份验证工作。 Users can register and login. 用户可以注册和登录。

What I'd like to do is set the $id parameter for the UserController's show() method with the value from Auth::user()->id; 我想做的是使用Auth::user()->id;的值为UserController的show()方法设置$id参数Auth::user()->id;

My thoughts behind this is, to not have to use id's in the routes. 我对此的想法是,不必在路由中使用id。

I'm pretty new to OOP, and php in general, so I'm not sure how to tackle this. 我对OOP和php来说还很陌生,所以我不确定该如何解决。

Thanks in advance for any tips or help! 在此先感谢您提供任何提示或帮助!

1st method 第一种方法

You can have a route with an optional user id: 您可以使用带有可选用户ID的路​​线:

Route::get('user/show/{id?}', 'UsersController@show')

If the show method of your controller doesn't get an id, it can use Auth::user() instead: 如果控制器的show方法没有ID,则可以使用Auth :: user()代替:

class UsersController extends BaseController {

    public function show($id = null) 
    {
        return View::make('userProfile')->with('user', $this->getCurrentUser($id));
    }

    public function getCurrentUser($id)
    {
        $this->currentUser = $id ? User::find($id) : Auth::user();
    }

}

Then in your view you will be able to always 这样一来,您将能够始终

{{ $user->name }}

2nd method 第二种方法

You could also have a BaseController which does that automatically for you using View::share(): 您还可以拥有一个BaseController,它可以使用View :: share()为您自动执行此操作:

class BaseController extends Controller {

    public function __construct()
    {
        parent::__construct();

        $this->shareUser();
    }

    public function shareUser($id = null)
    {
        View::share('user', $id ? User::find($id) : Auth::user());
    }

}

Then in your controller you don't need to pass the user: 然后,在您的控制器中,您不需要传递用户:

class UsersController extends BaseController {

    public function show() 
    {
        return View::make('userProfile');
    }

    public function thisIsAMethodOverridingIt($id)
    {
        $this->shareUser($id);

        return View::make('userProfile');
    }

}

It would even better to have this provided by a Service, but you'll have to read about Service Providers and Facades to make it happen. 最好由服务提供此功能,但您必须阅读有关服务提供商和外观的信息才能实现。

And you are still able to do that: 而且您仍然可以这样做:

{{ $user->name }}

Because View::share() will send that variable to all your views. 因为View::share()会将变量发送到所有视图。

3rd method 第三种方法

If you just need your user everywhere, use a global View::composer(): 如果只需要到处都有用户,请使用全局View :: composer():

View::composer('*', function($view)
{
    $view->with('currentUserName', Auth::check() ? Auth::user()->firstname : '');
});

You can put this in your routes.php or, better create a file for this purpose, something like app/composers.php and load it in your app/start/global.php: 您可以将其放在您的routes.php中,或者为此目的更好地创建一个文件,例如app / composers.php并将其加载到您的app / start / global.php中:

require app_path().'/composers.php';

As always, you can use it in your view, this way: 与往常一样,您可以通过以下方式在视图中使用它:

{{ $user->currentUserName }}

If you just need it for a couple of views, you can 如果您只需要几个视图,可以

View::composer(array('profile','dashboard'), function($view)
{
    $view->with('currentUserName', Auth::check() ? Auth::user()->firstname : '');
});

This is actually a recent problem that I encountered while working on an API. 这实际上是我在使用API​​时遇到的最新问题。

The way I handled it, was to introduce /me endpoints, so for example, you'd have: 我处理它的方式是引入/me端点,因此,例如,您将拥有:

Route::group(['before' => 'auth'], function() {
    Route::get('/user/show/{id}', 'UsersController@show');
    Route::get('/me', 'UsersController@show');
}

You'll notice that both routes point to the same code, but have different addresses. 您会注意到,两个路由都指向相同的代码,但是具有不同的地址。 This means that you can simplify requests by using the /me convention, without having to duplicate code. 这意味着您可以使用/me约定简化请求,而不必重复代码。 You'll also notice that I enclosed these in a group which applies the auth filter. 您还会注意到,我将它们封装在应用auth过滤器的组中。 This basically just makes sure that the user is authed, and while it may not be required for the first one, it'd definitely be required for the second. 基本上,这只是确保对用户进行了身份验证,尽管第一个用户可能不需要此身份,但第二个用户肯定是必需的。

Then your show method would look like this: 然后,您的show方法将如下所示:

public function show($id = false)
{
    $user = $this->getUserOrMe($id);

    return View::make('myview', ['user' => $user]);
}

This would require the below function: 这需要以下功能:

private function getUserOrme($id)
{
    return $id !== false ? User::find($id) : Auth::user();
}

A controllers methods should be accessed independently of each other, meaning that once the User object is returned, all the relevant code for the current request has access to it. 控制器方法应该彼此独立地访问,这意味着一旦返回User对象,当前请求的所有相关代码都可以对其进行访问。 Storing the User object in a class property would just over engineering. User对象存储在class属性中仅是工程上的事情。

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM