How to use Facades of Laravel outside its structure?

4

I'm trying to use some "laravel framework" classes in other files, but I'm not getting any success.

For example: I created a public/teste.php file with the following code:

require __DIR__.'/../bootstrap/autoload.php';

use Illuminate\Support\Facades\DB as DB;

$dbInstance = new DB;

var_dump($dbInstance);

This gives the following return:

  

Object (Illuminate \ Support \ Facades \ DB) # 2 (0) {}

But when I have the following code:

require __DIR__.'/../bootstrap/autoload.php';

use Illuminate\Support\Facades\DB as DB;

$dbInstance = new DB;

var_dump($dbInstance);

$pessoas = DB::table("pessoas")->get();

var_dump($pessoas);

It gives the following result:

 Fatal error: Uncaught exception 'RuntimeException' with message 'A facade root has not been set.' in C:\wamp\www\PROJETO\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php:210 
 Stack trace: 
 #0 C:\wamp\www\PROJETO\public\teste.php(11): Illuminate\Support\Facades\Facade::__callStatic('table', Array) 
 #1 C:\wamp\www\PROJETO\public\teste.php(11): Illuminate\Support\Facades\DB::table('pessoas') 
 #2 {main} thrown in C:\wamp\www\PROJETO\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php on line 210

Does anyone have any idea where I'm going wrong?

I'd like to use my Model too.

    
asked by anonymous 24.05.2016 / 03:57

1 answer

3

It's not that simple, Anderson. Laravel like any other framework usually needs to be initialized, as there are many other things that need to be running before using the classes themselves.

In the case of Laravel we have the Dependency Injection, Providers that call the files of Routes, Middlewares, etc.

To better understand how Laravel is started, read the next page in the documentation that talks about the lifecycle of Laravel. request.

In summary, do not need to reinvent the wheel . Understand and create your application from the Laravel framework.

If you only want to use a separate component of Laravel, such as from Database , you can use the Illuminate submodules. See the instructions for using the repository:

<?php

// Mudar aqui para o autoloader do composer
require 'vendor/autoload.php'

use Illuminate\Database\Capsule\Manager as Capsule;

$capsule = new Capsule;

$capsule->addConnection([
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'database',
    'username'  => 'root',
    'password'  => 'password',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
]);

// Set the event dispatcher used by Eloquent models... (optional)
use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;
$capsule->setEventDispatcher(new Dispatcher(new Container));

// Make this Capsule instance available globally via static methods... (optional)
$capsule->setAsGlobal();

// Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher())
$capsule->bootEloquent();

From here, you can use DB:: and your Models in Eloquent differently.

Note: This does not enable the use of Facades. Using them outside the framework is not possible because there are other integrations that need to be followed.

    
24.05.2016 / 04:14