dongxi1320 2015-04-15 17:34
浏览 7

Laravel 5.0命令

So after watching the following video about 8 times things started to click in my head. Now I'm somewhat understanding how the purpose of commands and how to deal with them either self handling or whether there's a handler class for performing this task. I still have a couple of questions though before I dive into trying to wrap my head around Events.

https://laracasts.com/lessons/laravel-5-commands

Questions

  1. From the way I've coded this. Does it look like I'm getting it or am I still missing something important?
  2. In the CreateUserCommandHandler with the call to the create method on the user repository inside the handle method is that how I should be sending my data to the repository?

Code

<?php namespace App\Http\Controllers;

use App\Repositories\Users\UserRepository;
use App\Repositories\Roles\RoleRepository;
use App\Commands\CreateUserCommand;
use App\Http\Requests;
use App\Http\Requests\UserRequest;
use Illuminate\HttpResponse;
use App\Http\Controllers\Controller;

class UsersController extends Controller {

    private $userRepository;
    private $roleRepository;

    /**
     * @param UserRepository $userRepository
     * @param RoleRepository $roleRepository
     */
    public function __construct(UserRepository $userRepository, RoleRepository $roleRepository)
    {
        $this->userRepository = $userRepository;
        $this->roleRepository = $roleRepository;
    }

    /**
     * Save a new user.
     *
     * @param UserRequest $request
     *
     * @return Response
     */
    public function store(UserRequest $request)
    {
        // formally had $this->userRepository->create($request->all());
        $this->dispatchFrom(CreateUserCommand::class, $request);

        return redirect('users');
    }

}

<?php namespace App\Commands;

use App\Commands\Command;

class CreateUserCommand extends Command {

    public $first_name;
    public $last_name;
    public $display_name;
    public $email;
    public $password;
    public $role_id;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct($first_name, $last_name, $display_name, $email, $password, $role_id)
    {
        $this->first_name = $first_name;
        $this->last_name = $last_name;
        $this->display_name = $display_name;
        $this->email = $email;
        $this->password = $password;
        $this->role_id = $role_id;
    }

}

<?php namespace App\Handlers\Commands;

use App\Commands\CreateUserCommand;

use App\Repositories\Users\UserRepository;
use Illuminate\Queue\InteractsWithQueue;

class CreateUserCommandHandler {

    /**
     * @var UserRepository
     */
    private $userRepository;

    /**
     * Create the command handler.
     *
     * @return void
     */
    public function __construct(UserRepository $userRepository)
    {
        //
        $this->userRepository = $userRepository;
    }

    /**
     * Handle the command.
     *
     * @param    $command
     * @return void
     */
    public function handle(CreateUserCommand $command)
    {
        $this->userRepository->create($command);
    }

}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 求帮我调试一下freefem代码
    • ¥15 R语言Rstudio突然无法启动
    • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
    • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
    • ¥15 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图