简体   繁体   English

如何在没有命名空间的情况下加载 Laravel 6 model 类

[英]how to load Laravel 6 model classes without namespace

I am new to Laravel and I was working on Laravel 4. I am trying to migrate to Laravel 6 on docker and I have the basic setup and Laravel project is up.我是 Laravel 的新手,我正在研究 Laravel 4。我正在尝试迁移到 Laravel 6 上的 docker,我有基本设置,Laravel 项目已经启动。

I created a table and a respective Eloquent Model in the models folder.我在模型文件夹中创建了一个表和相应的 Eloquent Model。 I am able to read the data in a controller.我能够读取 controller 中的数据。

namespace App\Http\Controllers;

use mysql_xdevapi\Exception;
use  App\Models\Card;

class welcomeController {
    public function show() {
        try {
            $cards = Card::all();
        } catch (\Exception $e) {
            die("Could not connect - " . $e );
        }

        print_r($cards); exit;
    }
}

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Card extends Model
{

}

In the older version of Laravel project, the 'Card::all()' was working without using USE command to import it.在 Laravel 项目的旧版本中,'Card::all()' 在不使用 USE 命令导入它的情况下工作。

I know namespaces are important, but wondering how it worked and how I can make replicate the same.我知道名称空间很重要,但想知道它是如何工作的以及如何使复制相同。

I do not know why you want to discard using namespaces, If you want to NOT use namespaces for your models, edit your composer.json like below我不知道你为什么要放弃使用命名空间,如果你不想为你的模型使用namespaces ,请像下面一样编辑你的composer.json

 "autoload": { "psr-4": { "App\\\\": "app/" }, "classmap": [ "database/seeds", "database/factories", "models" ] },

Make a directory in your root directory and add a new file ie 'models/Card.php'.在您的根目录中创建一个目录并添加一个新文件,即“models/Card.php”。

below should be the content of your Card.php下面应该是你的Card.php的内容

 <?php use \\Illuminate\\Database\\Eloquent\\Model; class Card extends Model { //database table here protected $table = "cards"; //Fillables protected $fillable = []; }

And in your controller WelcomeController在您的控制器WelcomeController

 <?php namespace App\\Http\\Controllers; use mysql_xdevapi\\Exception; class welcomeController { public function show() { try { $cards = \\Card::all(); } catch (\\Exception $e) { die("Could not connect - " . $e ); } print_r($cards); exit; } }

Do not forget the forward slash in your card model ie '\\Card::all()'不要忘记卡模型中的正斜杠,即 '\\Card::all()'

Make sure to move your models folder outside the app directory, Kindly note this is not php best practice确保将您的models文件夹移到app目录之外,请注意这不是php最佳实践

namespaces are just one of php worst feature.名称空间只是 php 最差的功能之一。 we had hardly got rid of the need to add 'include' or 'require' statements everywhere (thanks to autoloader) that now we have to add 'use' statements... Silly, especially on french keyboards where it takes one minute to reach the backslash key.我们几乎没有摆脱在任何地方添加“include”或“require”语句的需要(多亏了自动加载器),现在我们必须添加“use”语句……愚蠢的,尤其是在需要一分钟才能到达的法语键盘上反斜杠键。 Think about it: namespace are originally here for avoiding class names duplicates (which never occurred to me in more 25 years of programming of c++ and php)...想一想:命名空间最初是为了避免 class 名称重复(在我 25 年的 c++ 和 php 编程中从未想到过)......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM