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?

有帮助吗?

解决方案

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!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top