How can I use my own class in Laravel 5 view

I have a custom class that I have to use inside my view. But how am I doing this?

In Laravel 4.2, I just run composer.phar dump-autoloadand add start/local.phpas follows:

ClassLoader::addDirectories(array(
    app_path().'/commands',
    app_path().'/controllers',
    app_path().'/models',
    app_path().'/database/seeds',
    app_path().'/helpers/MyClass',
));

Finally, in my presentation I just use my class: MyClass::myMethod(). Again, how do I do this with Laravel 5?

thank

+4
source share
2 answers

You have two options: Serviceor Service Provider.

Service

This class can work as an assistant having all its static methods. For example, in the Application / Services folder, you can create a new one:

<?php
namespace Myapp\Services;

class DateHelper{

    public static function niceFormat(){
        return "This is a nice format";
    }

}

config/app.php :

'DateHelper' => 'Myapp\Services\DateHelper'

niceFormat(), \DateFormat::niceFormat();

, , , docs Facade.

+13

laravel blade

<?php  use MyApp\YourCustomClass; ?>
-1

All Articles