0

我正在尝试让 Sqlite3 在 laravel 上工作。

在一个简单的 PHP 文件中,它工作得很好!

<?php


$handle = new SQLite3("mydb.db");

?>

但是在 laravel 控制器的功能中,它严重失败。

  <?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');
    }
}

?>

它实际上甚至在我的 Sqlite3 对象上呈现了一条波浪线。

Class 'App\Http\Controllers\SQLite3' not found

为什么会这样?

4

2 回答 2

1

在控制器的顶部,您可以看到其他使用指令,添加以下声明:

Use SQLite3;

Laravel 由 PSR-4 命名空间驱动,它基本上指向目录结构中的一个文件,因此不同的库可以具有相同的类名,而不会相互影响。

除非您为 SQLite 类声明命名空间,否则它会认为该类与您的控制器位于同一文件夹中,因为这是您调用它的位置。

PHP 中包含的 SQLite3 类有一个以“SQLite3”开头的命名空间,因此通过在顶部声明它,对该类的任何引用都将指向正确的脚本。

于 2017-01-31T12:26:44.620 回答
0

这是一个命名空间问题。您在App\Http\Controllers名称空间中,其中不包含任何SQLite3类。

只需将其添加到use文件顶部的 if 语句列表中即可。

use SQLite3;

现在你不应该再得到那个错误了。

阅读手册中的更多内容:使用命名空间:别名/导入

于 2017-01-31T12:26:07.900 回答