mardi 16 février 2016

Autoload Helpers Functions Directory in Laravel 5

I've a class with a bunch of Date Helpers functions in it. I stored it at app\Helpers\DateHelper.php

Class

<?php

namespace App;

use DateTime;

class DateHelper {

    public static function day_ago($date) {

        if ($date) {

            $ts = time() - strtotime(str_replace("-","/", $date ));

            if($ts>31536000) $val = round($ts/31536000,0).' year';
            else if($ts>2419200) $val = round($ts/2419200,0).' month';
            else if($ts>604800) $val = round($ts/604800,0).' week';
            else if($ts>86400) $val = round($ts/86400,0).' day';
            else if($ts>3600) $val = round($ts/3600,0).' hour';
            else if($ts>60) $val = round($ts/60,0).' minute';
            else $val = $ts.' second';

            if($val>1) $val .= 's';

            return $val;


        }

    }

}


Composer.json

"autoload": {
    "classmap": [
        "database",
        "app/Helpers"
    ],
    "psr-4": {
        "App\\": "app/"
    },
    "files": ["app/Helper.php"]
},

Then, I run composer install

I got

Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Nothing to install or update
Generating autoload files
> php artisan clear-compiled
> php artisan optimize
Generating optimized class loader


Alias

Then, I added to the alias array like this

'DateHelper'  => 'app\Helpers\DateHelper',


Use

Then, I used it

{{ DateHelper::day_ago($n->created_at) }}


Result

Now, I kept getting `Class 'DateHelper' not found`

How do I properly add it ?

Any hints / suggestions on this will be much appreciated !



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire