5

我需要在扩展 LaravelValidator 的自定义 Laravel Validator 类中的某些验证方法中访问 Db 存储库。如何得到它?

我的类构造函数:

<?php namespace Pongo\Cms\Support\Validators;

use Illuminate\Validation\Validator as LaravelValidator;
use Pongo\Cms\Support\Repositories\PageRepositoryInterface as Page;
use Pongo\Cms\Support\Repositories\ElementRepositoryInterface as Element;

class PongoValidator extends LaravelValidator {

    /**
     * Class constructor
     * @param Page    $page 
     * @param Element $element
     */
     public function __construct($translator, $data, $rules, $messages, Page $page, Element $element)
     {
         $this->page = $page;
         $this->element = $element;
     }

     [...]

和我的验证器解析器(在 Laravel 文档中):

<?php

/**
 * Instantiate CustomValidator class
 */
Validator::resolver(function($translator, $data, $rules, $messages)
{
    return new Pongo\Cms\Support\Validators\PongoValidator($translator, $data, $rules, $messages);
});

如何让它发挥作用?谢谢

4

1 回答 1

6

好的...我找到了解决方案。

我的类构造函数:

<?php namespace Pongo\Cms\Support\Validators;

use Pongo\Cms\Support\Repositories\PageRepositoryInterface as Page;
use Pongo\Cms\Support\Repositories\ElementRepositoryInterface as Element;

use Illuminate\Validation\Validator as LaravelValidator;

use Config, Media, Str;

class PongoValidator extends LaravelValidator {

    private $page;

    private $element;

/**
 * Class constructor
 * @param Page    $page 
 * @param Element $element
 */
public function __construct($translator, $data, $rules, $messages, Page $page, Element $element)
{
    $this->translator = $translator;
    $this->data = $data;
    $this->rules = $this->explodeRules($rules);
    $this->messages = $messages;

    $this->page = $page;
    $this->element = $element;
}

[...]

和我的验证器解析器(与 IoC 一起救援!!):

<?php

use Pongo\Cms\Support\Validators\PongoValidator;

/**
 * Instantiate CustomValidator class
 */
Validator::resolver(function($translator, $data, $rules, $messages)
{
    $page = App::make('Pongo\Cms\Support\Repositories\PageRepositoryInterface');

    $element = App::make('Pongo\Cms\Support\Repositories\ElementRepositoryInterface'));

    return new PongoValidator($translator, $data, $rules, $messages, $page, $element);
});

希望它可以帮助其他人......再见

于 2013-10-08T15:02:44.343 回答