Reputation: 13
I am trying to get Sqlite3 to work on laravel.
In a simple Php file it works great!
<?php
$handle = new SQLite3("mydb.db");
?>
however inside a function of a laravel controller it fails badly.
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Contracts\Cookie\Factory;
class HomeController extends Controller
{
/*
|--------------------------------------------------------------------------
| Home Controller
|--------------------------------------------------------------------------
|
| This is the home - dasboard controller,
| where you land if you visit the site the first time
| ror are redirected from the login page.
|
*/
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('customauthorize');
}
public function Index(Request $request, Factory $cookie)
{
$handle = new SQLite3("mydb.db");
return view('welcome');
}
}
?>
It actually even presents a squiggly line on my Sqlite3 Object.
Class 'App\Http\Controllers\SQLite3' not found
Why does this happen?
Upvotes: 0
Views: 465
Reputation: 3849
At the top of your controller, where you see the other use directives, add the following declaration:
Use SQLite3;
Laravel is driven by PSR-4 namespacing, which basically points to a file within a directory structure so different libraries can have the same class names without stepping on each other's toes.
Unless you declare the namespace for the SQLite class, it thinks the class lives in the same folder as your controller because that is where you called it from.
The SQLite3 class that is included in PHP has a namespace that starts with 'SQLite3', so by declaring that at the top, any reference made to that class will point to the proper script.
Upvotes: 1
Reputation: 13635
It's a namespace issue. You are in the App\Http\Controllers
namespace, which doesn't contain any SQLite3
class.
Just add it to the list if use
statements in the top of your file.
use SQLite3;
Now you shouldn't get that error anymore.
Read more in the manual: Using namespaces: Aliasing/Importing
Upvotes: 0