PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, January 31, 2022

[FIXED] laravel creating a helper

 January 31, 2022     composer-php, laravel, php     No comments   

Issue

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?


Solution

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.



Answered By - Sougata Bose
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing