3
votes

I'm trying to use Laravel'4 Eloquent outside of the framework, since the Illuminate/Database package as been made availabile stand-alone via composer.

Eloquent itself is working fine, but I'm blocked trying to implement validation rules.

I've tried first with some pre-built library like Ardent and then with my own code but the result it's the same, I got this fatal-error:

Fatal error: Call to a member function make() on a non-object in vendor\illuminate\support\Illuminate\Support\Facades\Facade.php on line 177

The problem always start when I call Validator::make();

$validator = Validator::make(
    $this->attributes,
    array('name' => 'required')
);

Looking in debug it seems that static::resolveFacadeInstance('validator'); is called but it just return null.

I'm not familiar about how a Facades are meant to work, can anyone point me in the right direction? Thank you!

This is my composer.json:

{
    "require": {
        "illuminate/database": "~4.0",
        "illuminate/validation" : "~4.0",
        "laravelbook/ardent": "dev-master"
    },
    "config": {
        "preferred-install": "dist"
    },
    "minimum-stability": "dev"
}
2
Are installing it using Composer?Muhammad Usman
Yes, as I said Eloquent works well, I'll add my composer.json to the question...Ingro
What if you replace validation with "illuminate/validation": "4.0.*@dev" and database with "illuminate/database": "4.0.*@dev"?Muhammad Usman
Running "composer update" didn't update anything, so I guess I had already the latest version of the libraries. Anyway I don't think the problem lies in the libraries, but in how Laravel's facade system work. I think that I should initialize validator somewhere but I don't know how...Ingro
I think your problem is related to this issue: github.com/laravel/framework/issues/854Holger Weis

2 Answers

4
votes

For people who may need more information I'll report here the answer given to me on laravel's github repo by bencorlett (link):

// Store the factory instance somewhere, Maybe like:

class Validator {

    protected static $factory;

    public static function instance()
    {
        if ( ! static::$factory)
        {
            $translator = new Symfony\Component\Translation\Translator('en');
            static::$factory = new Illuminate\Validation\Factory($translator);
        }

        return static::$factory;
    }

    public static function __callStatic($method, $args)
    {
        $instance = static::instance();

        switch (count($args))
        {
            case 0:
                return $instance->$method();

            case 1:
                return $instance->$method($args[0]);

            case 2:
                return $instance->$method($args[0], $args[1]);

            case 3:
                return $instance->$method($args[0], $args[1], $args[2]);

            case 4:
                return $instance->$method($args[0], $args[1], $args[2], $args[3]);

            default:
                return call_user_func_array(array($instance, $method), $args);
        }
    }
}

$validator = Validator::make($data, $rules);
4
votes

I've got it working in a project that I'm working on. Unfortunately, the documentation on how to use it outside of the framework is non-existent. The documentation for how to use Eloquent outside of the framework at least exists, but is dodgy.

So first, put it in composer.json:

"illuminate/validation": "4.1.*",

The validation object is made by a factory object. This factory object is what the facade maps back to. So instead of using the facade, get that object like so:

$factory = new \Illuminate\Validation\Factory(new \Symfony\Component\Translation\Translator('en'));

You can then make your validation object:

$validator = $factory->make($inputArray, $rulesArray);

There are a couple of caveats to using this outside of the framework that I've found so far.

The first is that any database-dependent validations will fail, since it has no way to query the database. This is resolved by giving the validation object a Illuminate\Validation\DatabasePresenceVerifier object which wraps the database manager that you can get from the object that you made when setting up Eloquent:

$validator->setPresenceVerifier(new \Illuminate\Validation\DatabasePresenceVerifier($capsule->getDatabaseManager());

The other one is that the default validation messages are not set up. So far, I've handled this just by using the third argument to the make function to specify per-field custom rules.