• <legend id='C9Owb'><style id='C9Owb'><dir id='C9Owb'><q id='C9Owb'></q></dir></style></legend>
    <tfoot id='C9Owb'></tfoot>

  • <small id='C9Owb'></small><noframes id='C9Owb'>

      1. <i id='C9Owb'><tr id='C9Owb'><dt id='C9Owb'><q id='C9Owb'><span id='C9Owb'><b id='C9Owb'><form id='C9Owb'><ins id='C9Owb'></ins><ul id='C9Owb'></ul><sub id='C9Owb'></sub></form><legend id='C9Owb'></legend><bdo id='C9Owb'><pre id='C9Owb'><center id='C9Owb'></center></pre></bdo></b><th id='C9Owb'></th></span></q></dt></tr></i><div id='C9Owb'><tfoot id='C9Owb'></tfoot><dl id='C9Owb'><fieldset id='C9Owb'></fieldset></dl></div>
        • <bdo id='C9Owb'></bdo><ul id='C9Owb'></ul>

        在 Laravel 4 中使用命名空间

        Using namespaces in Laravel 4(在 Laravel 4 中使用命名空间)
          <legend id='O2jaT'><style id='O2jaT'><dir id='O2jaT'><q id='O2jaT'></q></dir></style></legend>

            1. <i id='O2jaT'><tr id='O2jaT'><dt id='O2jaT'><q id='O2jaT'><span id='O2jaT'><b id='O2jaT'><form id='O2jaT'><ins id='O2jaT'></ins><ul id='O2jaT'></ul><sub id='O2jaT'></sub></form><legend id='O2jaT'></legend><bdo id='O2jaT'><pre id='O2jaT'><center id='O2jaT'></center></pre></bdo></b><th id='O2jaT'></th></span></q></dt></tr></i><div id='O2jaT'><tfoot id='O2jaT'></tfoot><dl id='O2jaT'><fieldset id='O2jaT'></fieldset></dl></div>

              <small id='O2jaT'></small><noframes id='O2jaT'>

                <bdo id='O2jaT'></bdo><ul id='O2jaT'></ul>
                  <tbody id='O2jaT'></tbody>
                <tfoot id='O2jaT'></tfoot>
                  本文介绍了在 Laravel 4 中使用命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  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?

                  文件是 app/controllers/FilesController.phpapp/models/File.php.我正在尝试在 FilesController.php 中创建一个新的 File.

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

                  推荐答案

                  一旦掌握了命名空间的窍门,命名空间就非常容易了.

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

                  举个例子:

                  app/models/File.php

                  namespace AppModels;
                  
                  class File {
                  
                      public function someMethodThatGetsFiles()
                      {
                  
                      }
                  }
                  

                  app/controllers/FileController.php

                  namespace AppControllers;
                  
                  use AppModelsFile;
                  
                  class FileController {
                  
                      public function someMethod()
                      {
                  
                          $file = new File();
                      }
                  }
                  

                  声明命名空间:

                  namespace AppControllers;
                  

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

                  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 )

                  导入"其他命名空间:

                  use AppModelsFile;
                  

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

                  This Allows you to then use the File class without the Namespace prefix.

                  您也可以直接调用:

                  $file = new AppModelsFile();
                  

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

                  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.

                  完成后,您需要让他们运行 composer dump-autoload 以更新 Composer 的自动加载功能,以考虑您新添加的类.

                  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.

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

                  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:

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

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

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

                  查看 Namespaces 上的 PHP 文档,Nettut 的文档是这篇文章

                  Take a look at the PHP documentation on Namespaces and Nettut's is always a good resource with this article

                  这篇关于在 Laravel 4 中使用命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Laravel 4 - Connect to other database(Laravel 4 - 连接到其他数据库)
                  Call external API function from controller, LARAVEL 4(从控制器调用外部 API 函数,LARAVEL 4)
                  Empty string instead of null values Eloquent(空字符串而不是空值 Eloquent)
                  quot;laravel.logquot; could not be opened: failed to open stream(“laravel.log无法打开:无法打开流)
                  Displaying the Error Messages in Laravel after being Redirected from controller(从控制器重定向后在 Laravel 中显示错误消息)
                  Laravel Creating Dynamic Routes to controllers from Mysql database(Laravel 从 Mysql 数据库创建到控制器的动态路由)

                  <small id='8qTgT'></small><noframes id='8qTgT'>

                    <tbody id='8qTgT'></tbody>
                  • <legend id='8qTgT'><style id='8qTgT'><dir id='8qTgT'><q id='8qTgT'></q></dir></style></legend>

                    1. <tfoot id='8qTgT'></tfoot>
                          <bdo id='8qTgT'></bdo><ul id='8qTgT'></ul>

                          <i id='8qTgT'><tr id='8qTgT'><dt id='8qTgT'><q id='8qTgT'><span id='8qTgT'><b id='8qTgT'><form id='8qTgT'><ins id='8qTgT'></ins><ul id='8qTgT'></ul><sub id='8qTgT'></sub></form><legend id='8qTgT'></legend><bdo id='8qTgT'><pre id='8qTgT'><center id='8qTgT'></center></pre></bdo></b><th id='8qTgT'></th></span></q></dt></tr></i><div id='8qTgT'><tfoot id='8qTgT'></tfoot><dl id='8qTgT'><fieldset id='8qTgT'></fieldset></dl></div>