douyue8685 2015-07-04 07:58
浏览 6

laravel基础文件。 我这样做是否正确?

In Laravel 5.1 I need to pass some data to the register view. The getRegister() function in RegistersUsers trait is responsible to return the view.

At first I modified the function to pass my data but then I realized that the modifications would be overridden in case of an update.

So I made a new controller registerController and modified the route for getRegister like this: Route::get('auth/register', 'Auth\RegisterController@getRegister')

Inside the controller I redefined the getRegister function to return the view with my additional data. Now I am thinking.. am I doing this correctly or do I need to use some other method and use the original AuthController some other way?

Also, default auth is set to use email for post login, how do I change it to use username without touching the foundation files?

Are all these matters regarding "extending the framework" ?

Thanks

  • 写回答

1条回答 默认 最新

  • douduidui1046 2015-07-04 10:28
    关注

    First of all, it's always a bad idea to modify vendor files as the changes would be overwritten in case of any update in vendor package.

    Answering your first question:

    If you want to provide some additional data in registration view, you could do 2 things.

    First one is to add your own getRegister() method:

    public function getRegister()
    {
        return view('auth.register')->with(<parameter name>, <parameter value>);
    }
    

    The drawback of this solution is that in case of any future changes in trait's getRegister method those changes will not be incorporated into your controller.

    So the better approach is to reuse trait's getRegister() method in your controller and add your parameters to whatever trait returns:

    In your controller do:

    use RegistersUsers {
      RegistersUsers::getRegister as traitGetRegister;
    }
    
    public function getRegister()
    {
        return $this->traitGetRegister()->with(<parameter_name>, <parameter_value>);
    }
    

    Answering your second question:

    Laravel's Auth::attempt() method that is used to login users uses DatabaseUserProvider to load users from the DB and that provider is flexible enough to use any credential set you provide. However, if you want to use AuthenticatesUsers trait to login users, you have to override its postLogin method, because of the validation it does for user credentials:

    $this->validate($request, [
      'email' => 'required|email', 'password' => 'required',
    ]);
    

    UPDATE FOR LARAVEL 5.1: Since 5.1 there is no need to override postLogin() to change the column that is used to fetch users from the database. It is enough to set username property in the controller to the name of the column:

    public $username = 'login';
    
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP