简体   繁体   English

自定义帮助程序类未在Laravel 4中加载

[英]Custom helper class not loading in Laravel 4

I am currently running into a problem when trying to use a custom helper class in Laravel 4. 我目前在尝试在Laravel 4中使用自定义帮助程序类时遇到问题。
I've created a folder in app/libraries which has a custom class MenuComposer. 我已经在app/libraries创建了一个文件夹,其中包含一个自定义类MenuComposer。

app/libraries/folder/MenuComposer.php 应用程序/库/文件夹/ 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以自动加载app/libraries文件夹,并在控制台中运行dump-autoload命令。

composer.json 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 无论我如何尝试,Laravel都会一直返回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": 然后,Composer dump-autoload在您的“ / 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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM