Reputation: 671
In my public/index.php
of a Laravel 5 application, I have to query some fields in my database, so I used DB::talbe()
to do that.
But it returns error:
Fatal error: Class 'DB' not found in C:\xampp\htdocs\oceanboost\public\index.php on line 49
The code I used to call is:
$_active_plugins = DB::table("option")->where("key", "_active_plugins")->first();
I tried to use
$_active_plugins = \DB::table("option")->where("key", "_active_plugins")->first();
but the same error
And here is my full code of public/index.php
<?php
/**
* Laravel - A PHP Framework For Web Artisans
*
* @package Laravel
* @author Taylor Otwell <[email protected]>
*/
/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| our application. We just need to utilize it! We'll simply require it
| into the script here so that we don't have to worry about manual
| loading any of our classes later on. It feels nice to relax.
|
*/
require __DIR__.'/../bootstrap/autoload.php';
/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/
$app = require_once __DIR__.'/../bootstrap/app.php';
/*
|--------------------------------------------------------------------------
| Run The Application
|--------------------------------------------------------------------------
|
| Once we have the application, we can handle the incoming request
| through the kernel, and send the associated response back to
| the client's browser allowing them to enjoy the creative
| and wonderful application we have prepared for them.
|
*/
$_active_plugins = DB::table("option")->where("key", "_active_plugins")->first();
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$response = $kernel->handle(
$request = Illuminate\Http\Request::capture()
);
$response->send();
$kernel->terminate($request, $response);
My question is how to call built-in classes like DB
in a file when this file is not a class
Upvotes: 0
Views: 206
Reputation: 153140
It's not possible to use the DB facade at that point. The whole database component is booted from the Kernel at this part:
$response = $kernel->handle(
$request = Illuminate\Http\Request::capture()
);
Before that it's not available.
What you should do, is create a Service Provider for that. I won't go into all the details but you basically add a class that has a register
and an optional boot
method. You can create one with an artisan command:
php artisan make:provider PluginServiceProvider
In there you can use the DB facade like you want to. Then you just need to register that provider in config/app.php
by adding it to the long providers
array and your code will run before any routing or such happens.
Note that you should put your code into the boot
method since this one is called after all other providers have been register
ed.
Upvotes: 4
Reputation: 663
@Nguyen, if I am not mistaking, I think you are trying to query the database in the public/index.php
, which is a very big mistake when considering MVC Frameworks.
Please to do this, you should query your database in a controller then pass the results to a view which will be returned to the index.php.
And if you are in a dilemma, see laravel docs
Upvotes: 0