简体   繁体   English

在Laravel 4中使用名称空间

[英]Using namespaces in Laravel 4

I'm new to Laravel and using PHP namespaces in general. 我是Laravel的新手并且通常使用PHP命名空间。 I didn't run into any problems until I decided to make a model named File. 在我决定制作名为File的模型之前,我没有遇到任何问题。 How would I go about namespacing correctly so I can use my File model class? 我如何正确地进行命名空间,以便我可以使用我的File模型类?

The files are app/controllers/FilesController.php and app/models/File.php . 这些文件是app/controllers/FilesController.php app/models/File.phpapp/models/File.php I am trying to make a new File in FilesController.php . 我想在FilesController.php创建一个新File

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. 请记住,一旦你在一个Namespace中放置一个类来访问PHP的任何内置类,你需要从Root Namespace中调用它们。 eg: $stdClass = new stdClass(); 例如: $stdClass = new stdClass(); will become $stdClass = new \\stdClass(); 将成为$stdClass = new \\stdClass(); (see the \\ ) (见\\

"Import" other Namespaces: “导入”其他命名空间:

use App\Models\File;

This Allows you to then use the File class without the Namespace prefix. 这允许您使用不带Namespace前缀的File类。

Alternatively you can just call: 或者你也可以打电话:

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

But it's best practice to put it at the top in a use statement 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-autoload to 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 /文件请求指向控制器的someMethod()

Take a look at the PHP documentation on Namespaces and Nettut's is always a good resource with this article 看看关于命名空间的PHP文档,Nettut总是这篇文章的一个很好的资源

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的文档

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 Whatever \\ Model;

You probably have to do a dump-autoload with composer the first time around. 您可能必须第一次使用composer进行转储自动加载。

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 .....";
}
}

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. 在Laravel中,命名空间可以在作曲家可以自动加载的任何地方定义,我建议在app目录中定义名称空间。 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. 因此,您可以通过在app目录中创建Utils目录来定义像Utils这样的命名空间来保存Utility类,创建我们的实用程序类并像上面一样定义命名空间。

Afterwards you have run the command to ask composer to autoload classes: 然后你运行命令让作曲家去自动加载类:

 $ composer dump-autoload

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

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