在Laravel 4中创建命名空间

Al_*_*Al_ 5 php laravel laravel-4

我正在努力在Laravel 4中建立一个有效的名称空间,我已经在此处和代码亮本中阅读了一些指南。但我似乎仍然无法弄清楚。我的应用程序设置如下:

app / controllers / itemController app / services / itemValidator

在我的composer json文件中(每次更改时我都会进行转储-自动加载),我有:

"autoload": {
    "classmap": [
        <usual data...>
        "app/repositories",
        "app/services",
    ],
    "psr-0": {
        "Services": "app/services/"
    }
Run Code Online (Sandbox Code Playgroud)

我的项目控制器设置为:

<?php
use Services\ItemValidator;

class ItemsController extends BaseController {

public function store()
{
   $validator = new ItemValidator;
.....etc.....
Run Code Online (Sandbox Code Playgroud)

并且我的ItemsValidator类设置为:

 <?php namespace Services;

 class ItemValidator extends BaseValidator
 {
 ....code....
Run Code Online (Sandbox Code Playgroud)

当到达该$validator = new ItemValidator行时,这给了我以下错误:

Class 'Services\ItemValidator' not found 
Run Code Online (Sandbox Code Playgroud)

Ant*_*iro 5

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

"autoload": {
    "classmap": [
        <usual data...>
        "app/repositories",
        "app/services",       <---- this is the only entry needed to autoload your services
    ],
Run Code Online (Sandbox Code Playgroud)

那你应该执行

composer dump-autoload
Run Code Online (Sandbox Code Playgroud)

并检查您的班级是否出现在文件中

vendor/composer/autoload_namespaces.php
Run Code Online (Sandbox Code Playgroud)