Laravel使用存储库设置验证表单请求

时间:2015-08-12 17:39:47

标签: php validation laravel laravel-5

我正在使用Laravel 5.1,并希望使用存储库设置验证表单请求,而不是使用请求验证的标准方法。以下是我的设置:

我的路线:

Route::post('/users/administrators/{user}/edit', 'AdministratorController@update');

我的控制器(AdministratorController):

/**
 * Create a new instance.
 *
 * @return Collection
 */
public function __construct(UserRepoInterface $user)
{
    $this->user = $user;
}

/**
 * Update the specified resource in storage.
 *
 * @param  Request  $request
 * @param  int  $id
 * @return Response
 */
public function update($id)
{
    $this->user->updateUser($id);

    return Redirect::back()->withMessage('Your changes have been saved.');
}

我的模特(用户):

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    protected $table = 'users';

    public function showableFields()
    {
        return ['id', 'first_name', 'last_name', 'email', 'created_at'];
    }

    public function headers()
    {
        return ['Date'];
    }
}

我的界面(UserRepoServiceProvider):

<?php 

namespace App\Repos\User;

interface UserRepoInterface {

    /**
      * Update user.
      *
     * @param $user
     * @return mixed
     */
    public function updateUser($user);
}

我的界面方法(EloquentUser):

<?php 

namespace App\Repos\User;

use Illuminate\Database\Eloquent\Model;
use App\Models\User;
use App\Models\UsersRoles;

class EloquentUser implements UserRepoInterface
{
    protected $user, $roles;

    /**
     * Create a new EloquentUser repository instance.
     *
     * @return Collection
     */
    public function __construct(User $user, UsersRoles $roles)
    {

        $this->user     = $user;
        $this->roles    = $roles;
    }

    /**
     * Update user.
     *
     * @param $user
     * @return mixed
     */
    public function updateUser($user)
    {

    }

}

我在/config/app.php中指定服务提供程序:

App\Providers\UserRepoServiceProvider::class

我正在使用以下内容启动提供程序:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\Repos\User\EloquentUser;

class UserRepoServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        $this->app->bind('App\Repos\User\UserRepoInterface', function($app) 
        {
            return new EloquentUser(
                new \App\Models\User,
                new \App\Models\UsersRoles
            );
        });
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

验证表单请求的最有效和最有效的方法是什么?它是正常使用请求验证还是以某种方式使这个过程变得动态?

欢迎任何建议。

0 个答案:

没有答案