在 Laravel 4 中创建命名空间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16984457/
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-09-14 07:55:05  来源:igfitidea点击:

Creating a Namespace in Laravel 4

phplaravellaravel-4

提问by Al_

I am struggling to set up a working name space in Laravel 4, I've read some guides on here and in the code bright book. But I still can't seem to figure it out. My app is set up as follows:

我正在努力在 Laravel 4 中设置一个工作名称空间,我已经阅读了这里和代码明亮书中的一些指南。但我似乎还是想不通。我的应用程序设置如下:

app/controllers/itemController app/services/itemValidator

app/controllers/itemController app/services/itemValidator

in my composer json file (which I dump-autoload evertime it gets changed) I have:

在我的作曲家 json 文件(我每次更改时都会转储自动加载)我有:

"autoload": {
    "classmap": [
        <usual data...>
        "app/repositories",
        "app/services",
    ],
    "psr-0": {
        "Services": "app/services/"
    }

my items controller is set up as:

我的物品控制器设置为:

<?php
use Services\ItemValidator;

class ItemsController extends BaseController {

public function store()
{
   $validator = new ItemValidator;
.....etc.....

and my ItemsValidator class is set up as:

我的 ItemsValidator 类设置为:

 <?php namespace Services;

 class ItemValidator extends BaseValidator
 {
 ....code....

Which gives me the following error when it gets to the $validator = new ItemValidatorline:

当它到达$validator = new ItemValidator线路时,这给了我以下错误:

Class 'Services\ItemValidator' not found 

采纳答案by Antonio Carlos Ribeiro

Just to clarify: according to comments, what works in this case is a composer.json formatted this way:

只是为了澄清:根据评论,在这种情况下有效的是一个以这种方式格式化的 composer.json :

"autoload": {
    "classmap": [
        <usual data...>
        "app/repositories",
        "app/services",       <---- this is the only entry needed to autoload your services
    ],

Then you should execute

那么你应该执行

composer dump-autoload

And check if your class appeared in the file

并检查您的课程是否出现在文件中

vendor/composer/autoload_namespaces.php

回答by user3875639

app/admin/foocontroller.php

应用程序/管理员/foocontroller.php

in composer.json:

在 composer.json 中:

"psr-0":{
    "App\Controller\Admin" : "app/controller/admin"
}

composer dump-autoload -o

作曲家转储自动加载 -o

foocontroller.php

foocontroller.php

<?php namespace App\Controller\Admin;
class foocontrolelr extends \BaseController
{
    public function index(){}
}

回答by SamSquanch

what I believe you want to do is

我相信你想做的是

<?php namespace Services;

 class ItemValidator extends Basevalidator
 { 
  .....

Then instead of using the class name in the name space, use the name space then extend the validator

然后不是在命名空间中使用类名,而是使用命名空间然后扩展验证器

<?php
  uses Services;

  class ItemsController extends ItemValidator
  { 

I believe thats what you are trying to accomplish.

我相信这就是你想要完成的。