dongzhuohan7085
2014-05-07 08:23
浏览 66
已采纳

如何在laravel 4.1中的控制器中使用不同的命名空间

What I want to do is to use different namespaces in a controller. I have a tree scheme like this:

app/
app/controllers/
app/modules/
app/modules/modulename/
app/modules/modulename/controllers/
app/modules/modulename/controllers/modulecontroller.php
app/modules/modulename/models/
app/modules/modulename/models/modulemodel.php

What I want to do is to call a model from a controller in app/controllers/ folder. Therefore I am supposed to add namespace as follows:

    use App\Modules\Facebook\Controllers\Facebook;

The problem is that when I add a namespace and use App::() function at the sametime, I get the following error:

    Class 'App\Modules\Modulename\Controllers\App' not found

I think it is looking the App::() function in module folder. How can I solve this problem?

图片转代码服务由CSDN问答提供 功能建议

我想要做的是在控制器中使用不同的命名空间。 我有这样的树形图:

  app / 
app / controllers / 
app / modules / 
app / modules / modulename / 
app / modules / modulename / controllers /  \ NAPP /模块/模块名/控制器/ modulecontroller.php \ NAPP /模块/模块名/型号/ \ NAPP /模块/模块名/型号/ modulemodel.php 
   
 
 

我想要做的是从app / controllers /文件夹中的控制器调用模型。 因此我应该按如下方式添加命名空间:

 使用App \ Modules \ Facebook \ Controllers \ Facebook; 
   
 
 < 问题是当我在同一时间添加命名空间并使用App ::()函数时,我收到以下错误: 
 
 
  Class'App \ Modules \ Modulename  \ Controllers \ App'找不到
   
 
 

我认为它正在模块文件夹中查找 App ::()函数。 我该如何解决这个问题呢?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doutenglou6588 2014-05-07 08:36
    已采纳

    if you use App inside your App\Modules\Facebook\Controllers namespace, it will be interpreted as App\Modules\Facebook\Controllers\Facebook\App class.

    since you don't want to have the previous namespace, you use a \ before App like:

    \App::()

    or put a use statement of top the class like use App;

    打赏 评论
  • doyp9057 2014-05-07 08:26

    if you say

    use App\Modules\Facebook\Controllers\Facebook;
    

    then you are supposed to use Facebook instead of App... Or don´t I understand your problem correctly?

    if you say

    use App\Modules\Facebook\Controllers\Facebook as FacebookController;
    

    the you can use FacebookController in your file

    if you need Access to the root App, you need to to root it using a leading \

    \App::make()
    
    打赏 评论
  • dongxifu5009 2014-05-07 08:32

    You probably are creating an unusual namspace scheme. It appears you are namespacing every class from your module differently. You should namespace your code within your module only, like so:

    // Adding Onur to the namespace prevents any future namespace collisions.
    <?php namespace Onur\Facebook;
    

    After creating your namespace you should add all classes that are outside of your namespace that you want to use as followed.

    use Eloquent, Input, Validate, Etc;
    

    This prevents you from adding a \ in front of every class instance, making your code hard maintain and prone to errors. It also gives you a good overview on all the classes you are using in the current class.

    打赏 评论

相关推荐 更多相似问题