Domanda

I am currently running into a problem when trying to use a custom helper class in Laravel 4.
I've created a folder in app/libraries which has a custom class MenuComposer.

app/libraries/folder/MenuComposer.php

<?php
    namespace 'folder\MenuComposer'

    class MenuComposer {
      // Code here
    }

I've edited composer.json to autoload the app/libraries folder and ran the dump-autoload command in console.

composer.json

    "autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/models",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php",
        "app/libraries"
    ]
},

And finally I call the class like so:

View::composer('layouts.back', 'folder/MenuComposer');

Whatever I try, Laravel keeps returning the message Class 'MenuComposer' not found

Does anyone here know what the problem might be?

È stato utile?

Soluzione

Your namespace should be declared as the following rather than with quotes:

namespace folder\MenuComposer;

Composer dump-autoload then generates the following in your "/vendor/composer/autoload_classmap":

'folder\\MenuComposer\\MenuComposer' => $baseDir . '/app/libraries/folder/MenuComposer.php'

Which would indicate the class can be reached at:

folder/MenuComposer/MenuComposer

Hope this helps!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top