Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Namespace in 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 
like image 982
Al_ Avatar asked Jun 07 '13 12:06

Al_


1 Answers

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
like image 200
Antonio Carlos Ribeiro Avatar answered Oct 10 '22 08:10

Antonio Carlos Ribeiro