将数据传递到Laravel 5中的FormRequest

时间:2015-10-08 13:48:27

标签: php laravel laravel-5

在Laravel 5中,如何将数据传递到FormRequest类?

背景

我正在尝试验证来自HTML select元素的get变量。

我这样做是通过继承FormRequest并添加到rules数组。我想从文件列表中动态构建此验证规则以保持DRY。

将此列表传递给FileFormRequest的最佳方法是什么?

app / Http / Controllers / UploadController.php(请原谅公共财产)

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Requests\FileFormRequest;
use App\Http\Controllers\Controller;
use File;

class UploadController extends Controller
{

    public $files = [];

    public function get(FileFormRequest $request)
    {
        $files_dir = storage_path('app').'/spreadsheets';
        foreach (File::allFiles($files_dir) as $file) {
            $file = $file->getBasename();
            $this->files[$file] = $file;
        }

        return view('main', array(
            'title' => '',
            'file_names' => $this->files,
            'file_name' => '',
            'sheet_name' => '',
            'encoding' => 'UTF-8'
        ));
    }

}

应用程序/ HTTP /请求/ FileFormRequest.php

<?php namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Response;

class FileFormRequest extends FormRequest
{

    public function rules()
    {
        // What goes here in place of the empty array?
        $files = implode(',', []);

        return [
            'file_name' => 'sometimes|required|in:'.$files,
        ];

    }

    public function authorize()
    {
        return true;
    }
}

2 个答案:

答案 0 :(得分:0)

你可以试试这个:

public function rules()
{
    // Get the REQUEST (GET/POST) variable
    $files = app('request')->get('form_field_name');

    return [
        'file_name' => 'sometimes|required|in:'.$files,
    ];

}

如果form_field_name不是数组,则根据其包含的值将其设为数组。此外,如果需要大量代码,您可以创建另一个protected方法来从请求获取输入,例如:

protected function getRequestInput()
{
    $files = app('request')->get('form_field_name');

    // Process it here and finally return, i.e:
    return $files; // array
}

因此,您现在可以在rules方法中调用该方法,如下所示:

$files = $this->getRequestInput();

答案 1 :(得分:0)

好的,我通过

解决了这个问题
  • 将上传文件列表功能移动到实用程序界面和类
  • 使用服务提供者将实用程序类的实例公开为服务
  • 将服务注入Controller和FormRequest类

代码是:

app / Contracts / Utilities / UploadFilesContract.php - 创建界面

<?php

namespace App\Contracts\Utilities;

interface UploadFilesContract {

    public function __construct($dir);

    public function getDir();

    public function getFilesBasenames();

}

app / Utilities / UploadFiles.php - 创建具有列表功能的类

<?php

namespace App\Utilities;

use Illuminate\Support\Facades\File;
use App\Contracts\Utilities\UploadFilesContract;

class UploadFiles implements UploadFilesContract {

    private $dir;
    private $files;

    public function __construct($dir = '')
    {
        $this->dir = storage_path('app').(0 < strlen($dir) ?  '/'.$dir : '');
        $this->files = File::allFiles($this->dir);
    }

    public function getDir()
    {
        return $this->dir;
    }

    public function getFilesBasenames()
    {
        $files = [];
        foreach ($this->files as $file) {
            $file = $file->getBasename();
            $files[$file] = $file;
        }
        return $files;
    }

}

app / Providers / UploadFilesServiceProvider.php - 将接口绑定到类的实例

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\Utilities\UploadFiles;

class UploadFilesServiceProvider extends ServiceProvider
{
    protected $defer = true;

    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bindShared('App\Contracts\Utilities\UploadFilesContract', function($app) {
            return new UploadFiles('spreadsheets');
        });
    }

    public function provides()
    {
        return ['App\Utilities\UploadFiles'];
    }
}

app / Http / Controllers / UploadController.php - 在构造函数中注入上传的文件界面

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Requests\FileFormRequest;
use App\Contracts\Utilities\UploadFilesContract;

class UploadController extends Controller
{

    private $upload_files;

    public function __construct(UploadFilesContract $upload_files) {
        $this->upload_files = $upload_files;
    }

    public function get(FileFormRequest $request)
    {   
        return view('main', array(
            'title' => '',
            'file_names' => $this->upload_files->getFilesBasenames(),
            'file_name' => '',
            'sheet_name' => '',
            'encoding' => 'UTF-8'
        ));
    }

}

app / Http / Requests / FileFormRequest.php - 在构造函数中注入上传的文件界面

<?php namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use App\Contracts\Utilities\UploadFilesContract;

class FileFormRequest extends FormRequest
{
    private $upload_files;

    public function __construct(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null, UploadFilesContract $upload_files)
    {
        parent::__construct($query, $request, $attributes, $cookies, $files, $server, $content);
        $this->upload_files = $upload_files;
    }

    /**
     * @return array
     */
    public function rules()
    {
        $files = implode(',', $this->upload_files->getFilesBasenames());

        $rules = [
            'file_name' => 'sometimes|required|in:'.$files,
        ];

        return $rules;
    }

    /**
     * @return bool
     */
    public function authorize()
    {
        return true;
    }
}

config / app.php - 注册提供者

<?php

// ...

'providers' => [

    // ...

    App\Providers\UploadFilesServiceProvider::class,

],

// ...