How to use Laravel Facades outside of its structure?

Asked

Viewed 982 times

4

I am trying to use some classes of the "Laravel structure" in other files, but I am not succeeding.

For example: I created a file public/teste.php 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);

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 would also like to use my Model.

1 answer

4


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 Dependency Injection, Providers that call the files Routes, Middlewares, etc.

To better understand how Laravel is initiated, read the next page of the documentation that talks about the life cycle of the request.

In short, no need to reinvent the wheel. Understand and create your application from the structure of Laravel.

If you only want to use a separate Laravel component, such as database, it is possible to use the Illuminate sub-modules. See the instructions for use taken from 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 there, you can use differently the DB:: and their Models in Eloquent.

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

  • I read the documentation you mentioned and I still can’t fully understand the life cycle of a Portable application. For my problem, Telvez I need to use this class here "Databasemanager", isn’t it also simple used?? My code is not inside the controller because I want to use a Framework I made, and I found it very complicated to incorporate it inside the Controller, so I want to use it from the outside... That’s why I’m trying to access the database from outside the controllers.

  • In your case you can only use the database component https://github.com/illuminate/database

  • @gmsantos seems to me that Laravel, despite being separated, as the very name of the repositories on github says is [Readonly]

  • @gmsantos have you managed to use this component there? I’m not getting it.. You are giving the following error: Fatal error: Class 'Illuminate Database Capsule Manager' not found .... I followed the documentation but I was not happy. Like you did?

  • I got it, it was missing: require .... '/vendor/autoload.php'?

  • 1

    @Wallacemaxters is a split of the main Ref. It does not accept contributions from that Ref, only in the main... so read-only

  • @Andersonsouza later I edit the answer.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.