dtiu94034 2013-06-07 04:43 采纳率: 0%
浏览 65
已采纳

在Laravel 4中创建命名空间

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:

app/controllers/itemController app/services/itemValidator

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

"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:

 <?php namespace Services;

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

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

Class 'Services\ItemValidator' not found 

展开全部

  • 写回答

3条回答 默认 最新

  • douzhiji2020 2013-06-07 05:52
    关注

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

    "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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部