php 在 Laravel 4 中使用命名空间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14714848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:50:39  来源:igfitidea点击:

Using namespaces in Laravel 4

phpnamespaceslaravellaravel-4

提问by user2030045

I'm new to Laravel and using PHP namespaces in general. I didn't run into any problems until I decided to make a model named File. How would I go about namespacing correctly so I can use my File model class?

我是 Laravel 的新手,一般使用 PHP 命名空间。在我决定制作一个名为 File 的模型之前,我没有遇到任何问题。我将如何正确进行命名空间以便我可以使用我的 File 模型类?

The files are app/controllers/FilesController.phpand app/models/File.php. I am trying to make a new Filein FilesController.php.

这些文件是app/controllers/FilesController.phpapp/models/File.php。我正在尝试FileFilesController.php.

回答by Josh Holloway

Namespacing is pretty easy once you get that hang of it.

一旦你掌握了它,命名空间就很容易了。

Take the following example:

以下面的例子为例:

app/models/File.php

应用程序/模型/File.php

namespace App\Models;

class File {

    public function someMethodThatGetsFiles()
    {

    }
}

app/controllers/FileController.php

应用程序/控制器/FileController.php

namespace App\Controllers;

use App\Models\File;

class FileController {

    public function someMethod()
    {

        $file = new File();
    }
}

Declare the Namespace:

声明命名空间:

namespace App\Controllers;

Remember, once you've put a class in a Namespace to access any of PHP's built in classes you need to call them from the Root Namespace. e.g: $stdClass = new stdClass();will become $stdClass = new \stdClass();(see the \)

请记住,一旦将类放入命名空间以访问任何 PHP 内置类,您就需要从根命名空间调用它们。例如:$stdClass = new stdClass();将成为$stdClass = new \stdClass();(见\

"Import" other Namespaces:

“导入”其他命名空间:

use App\Models\File;

This Allows you to then use the Fileclass without the Namespace prefix.

这允许您使用File没有命名空间前缀的类。

Alternatively you can just call:

或者,您可以致电:

$file = new App\Models\File();

But it's best practice to put it at the top in a usestatement as you can then see all the file's dependencies without having to scan the code.

但是最好将它放在use语句的顶部,因为这样您就可以看到所有文件的依赖项而无需扫描代码。

Once that's done you need to them run composer dump-autoloadto update Composer's autoload function to take into account your newly added Classes.

完成后,您需要运行它们composer dump-autoload以更新 Composer 的自动加载功能,以将您新添加的类考虑在内。

Remember, if you want to access the FileController via a URL then you'll need to define a route and specify the full namespace like so:

请记住,如果您想通过 URL 访问 FileController,那么您需要定义一个路由并指定完整的命名空间,如下所示:

Route::get('file', 'App\Controllers\FileController@someMethod');

Which will direct all GET /file requests to the controller's someMethod()

这会将所有 GET /file 请求定向到控制器的 someMethod()

Take a look at the PHP documentation on Namespacesand Nettut's is always a good resource with thisarticle

看看在PHP文件命名空间和Nettut的始终是一个很好的资源文章

回答by Pierre Broucz

first, load your class with:

首先,加载您的类:

$ composer dump-autoload

then

然后

$file = new File;

// your stuff like:
$file->name = 'thename';
$file->active = true;

$file->save();

Section: Insert, Update, Delete on Laravel 4 Eloquent's doc

部分:Laravel 4 Eloquent 文档中的插入、更新、删除

回答by ipixelsmediaworks

To namespace your model, at the top of your model class right after the opening

要命名您的模型,请在模型类的顶部打开后立即

Then when you call from controllers you will call new Whatever\Model;

然后当您从控制器调用时,您将调用 new What\Model;

You probably have to do a dump-autoload with composer the first time around.

您可能必须在第一次使用 composer 进行转储自动加载。

回答by Muhammad Saeed

have a look to it.. hopefully will clear your query....

看看它.. 希望能清除您的查询..

<?php

 namespace app\controllers;
 use yii\web\Controller;
 use app\models\users;
  class UserController extends Controller{
 public function actionIndex()
 {
echo "working on .....";
}
}

回答by Eric McWinNEr

Namespaces are defined at the top of PHP classes right after the opening php script tag like this:

命名空间在 PHP 类的顶部定义在打开 php 脚本标签之后,如下所示:

 <?php
   namespace MyNameSpace;

When you then want to use the namespaced class in some other class, you define it like this:

当您想在其他类中使用命名空间类时,您可以像这样定义它:

new MyNameSpace\PhpClass;

or import it at the top of the file (after namespaces if present) like this:

或将其导入文件顶部(如果存在命名空间之后),如下所示:

 <?php

   //namespace

   use MyNameSpace\MyPHPClass;

   //then later on the code you can instantiate the class normally
   $myphpclass = new MyPHPClass();

In Laravel namespaces can be defined anywhere composer can autoload them, I'd recommend defining namespaces within the app directory. So you can define a namespace like Utils for holding Utility classes by creating a Utils directory in the app directory, creating our utility classes and defining the namespace as we did above.

在 Laravel 命名空间中,可以在 composer 可以自动加载它们的任何地方定义命名空间,我建议在 app 目录中定义命名空间。因此,您可以通过在 app 目录中创建一个 Utils 目录,创建我们的实用程序类并定义命名空间来定义一个像 Utils 这样的命名空间来保存实用程序类。

Afterwards you have run the command to ask composer to autoload classes:

之后,您运行命令要求作曲家自动加载类:

 $ composer dump-autoload