سؤال

I have created file helpers.php (app/helpers.php)

function test($var)
{
    return $var;
}

I have changed app/start/global.php into

ClassLoader::addDirectories(array(

    app_path().'/commands',
    app_path().'/controllers',
    app_path().'/models',
    app_path().'/database/seeds',
    app_path().'/helpers.php',

));

Also composer.json into

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

But when am I trying to call this function inside controller

$data = Test::all();
return test($data);

It throws me an error

Call to undefined function test()

What's wrong?

هل كانت مفيدة؟

المحلول

add -

require app_path().'/helpers.php';

at the end of - app/start/global.php you dont have to add it to composer.json and set -

ClassLoader::addDirectories(array(

app_path().'/commands',
app_path().'/controllers',
app_path().'/models',
app_path().'/database/seeds',
));

hope this will work.

نصائح أخرى

@sgt's approach works fine, but if you still want to use composer, use the files autoloader.

composer.json

"autoload": {
    ...
    "files": [
        "path/to/my/file.php"
    ]
    ...
}

Don't forget to composer dump-autoload after doing a change to the autoload directive.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top