leonyu's avatar

[L5] How to add my own custom class ?

Ok, in laravel 4, if I want to add my own custom class, eg : library\myFunction.php then I do the following steps :

add "myFunction.php" into app/myfolder/myFunction.php
at app/start/global.php , within ClassLoader::addDirectories(array( , I add app_path().'/myfolder',
And to call it within my blade view, I add the following codes :

<?php
  $FmyFunction1 = new myFunction;
  $is_ok1=($FmyFunction1->is_ok());
?>

And it works in Laravel 4.

But how to do so in Laravel 5 ???

0 likes
10 replies
vjacob's avatar

if your file is just a file with functions, just add it to your composer.json

},
    "autoload": {
        "files": [
            "app/myfolder/myFunction.php"
        ]
    },

then do a composer dump-autoload

if it is a php class, then you should be using psr-4 autoloading

"autoload": {
        "psr-4": {
            "MyApp\\": "app/"
        },
    },

read up on psr-4 autoloading if you're not familiar http://www.php-fig.org/psr/psr-4/

3 likes
bestmomo's avatar

With L5 just create a directory in App like App/MyClasses and use namespaces (App\MyClasses...) to get all work fine without other manipulations because App is in PSR-4.

4 likes
leonyu's avatar

Here are my steps :

The content for app/library/myFunctions.php is below :

<?php namespace App\library {
    class myFunctions {
        public function is_ok() {
            return 'myFunction is OK';
        }
    }
}
?>

At resources/views/test.blade.php , I added :

<?php
  $FmyFunctions1 = new myFunctions;
  $is_ok = ($FmyFunctions1->is_ok());
?>

Then at composer.json within "autoload": I added :

    "files": [
        "app/library/myFunctions.php"
    ],

And within app\Http\Controllers\HomeController.php at public index() I add :

return view('test');

But it always give me error messages regarding 'myFunctions' as below :

Whoops, looks like something went wrong. 1/1 FatalErrorException in xxxx line 7: Class 'myFunctions' not found in xxxx line 7 at HandleExceptions->handleShutdown()

2 likes
bestmomo's avatar
Level 52

Hello,

No need to change composer. Just have good play with namespaces :

<?php
  $FmyFunctions1 = new \App\library\myFunctions;
  $is_ok = ($FmyFunctions1->is_ok());
?>
5 likes
wiesson's avatar

It works for me like @bestmomo said. I've created a php file for my class in app/Libraries and added the namespace namespace App\Libraries;. In the blade template, I've just loaded the class with App\Libraries\Classname.

E.g. <span class="glyphicon glyphicon-{{ App\Libraries\Utils::energyIcon($point->EnergyMeterTypeID) }}"></span>

devninja's avatar

i did created a class file and added it in composer :

        "classmap": [
            "app/Libraries",

then did a dump-autoload is this practice correct ? (i'm on Laravel 4.2)

clarenswd's avatar

it works to certain point, (in my app) which I am confused. The namespacing method under App\ works perfectly, I can get inside my classes but then I have an issue, inside on of my classes I use this new SimpleXMLElement("....");

and then it crashes. Any ideas?

anizou's avatar

!!!!!!!! What if the custom class contains alot of class on it , or it uses the

$method = new ReflectionMethod($this->myClass, $methodName); in exemple

1 like

Please or to participate in this conversation.