3

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 keep getting Class 'DateHelper' not found.

How do I properly add it ?

9
  • 1
    do a composer dump-autoload. And just call day_ago($n->created_at); Commented Feb 16, 2016 at 17:11
  • Let me try, I thought composer install include that already. Commented Feb 16, 2016 at 17:12
  • But in this case, you don't have to create a date helper function. Why don't you use Carbon ? Commented Feb 16, 2016 at 17:12
  • I have other functions that I would like to make a use out of it. So eventually, I need to learn how to integrate a helpers function in my L5 application. Commented Feb 16, 2016 at 17:14
  • Sure, so whenever you create a helper class, just include the path in the composer.json and do composer dump-autoload. Then you don't have to reference the class again. Commented Feb 16, 2016 at 17:15

2 Answers 2

3

Your alias is wrong. The alias is meant to be the fully qualified class name with namespace, not the directory it's in.

'DateHelper'  => 'App\DateHelper',

I would suggest following PSR-4 standards. It would save time and minimize confusion.

Additionally, so this doesn't happen again, it would be better to use syntax like the following...

'DateHelper'  => App\DateHelper::class,

That way, you can be absolutely sure the class exists.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok. So how do we make our directory full of helpers classes auto load ?
Set the correct namespaces on them. For example, your DateHelper should have the namespace App\Helpers. If your namespaces follow the directory structure, PSR-4 will autoload them with no changes required to composer.json and no need to composer dump-autoload. Keep in mind if you change the namespace though, make sure to update the alias to reflect the change in namespace.
1

A typical example for a helper file in Laravel:

  1. Create a helper.php file in your app directory.
  2. Create your helper functions
  3. Now add the helper.php file in your composer.json file.
  4. Do a composer dump-autoload
  5. Now your helper function will be available throughout your project.

If you are creating a directory for the helper files, then namespace the helperfiles.

"autoload": {
  "classmap": [
     "database",
   ],
   "psr-4": {
      "App\\": "app/",
      "Helpers\\": "app/helpers/" //This is if you are using the directory
   },
   "files": ["helper.php"] //This is if it's just a php file.
},

i.e :

//app/helpers/helperClass.php
<?php namespace Helpers;

class helperClass{
  public function showDate()
  {
    //return
  }
}

In your controller, when you use the helper function, import the class.

i.e:

use Helpers/helperClass;
//If you've creates an alias for this, use it here. 
use helperClass;   //(This is from the config/app.php file)

If it's a view, use it like: {{ \Helpers\helperClass::showDate() }}

19 Comments

I know that steps will work because I've done that. My goal is to autoload the whole directory not just 1 file which is helper.php.
You can include "Helpers": "app/helpers" directory in your psr-4 section of the composer.json and namespace your helper files with Helpers. Also do a composer dump-autoload
Ohh psr-4, I thought it was classmap section. I'll change it and update you.
Here my autoload section, "autoload": { "classmap": [ "database" ], "psr-4": { "App\\": "app/", "app/Helpers" }, "files": ["app/Helper.php"] },
Also, if you are making your helper functions inside a class, make an alias in the config file and reference the class whereever you use it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.