'Where do I put my custom classes and functions in Laravel [duplicate]

I'm trying to build a class with some frequently used functions that I can use from anywhere in my project. I don't know where to build the PHP file with the classes in it or how to recall them... Can anyone help me figure out where all this stuff fits in? THANKS!!!

/App/Http/Helpers/MyClasses.php

<?php

class PopularFunctions {
  public function sayhi() {
    echo 'hi';
  }
}

?>

/App/Http/Controllers/TasksController.php

<?php

namespace App\Http\Controllers;

use App\Http\Helpers\MyClasses;

class TasksController extends Controller {

  public function index() {

    $myfunctions = new PopularFunctions();
    $myfunctions->sayhi();

  }

}

This returns: Class 'App\Http\Controllers\PopularFunctions' not found.



Solution 1:[1]

Basically, we could create another directory within app directory (MyCustomStuff for example), and then namespace our files correctly.

I know of two methods.

1. Global-Function Through Composer

App/MyCustomStuff/MyGlobalTools.php

<?php
function sayhi() {
    echo 'hi';
}
?>

then in composer.json in "autoload": { } add

"files": [
    "app/MyCustomStuff/MyGlobalTools.php"
]

so the structure will be

"autoload": {
    "classmap": [
        "database/seeds",
        "database/factories"
    ],
    "psr-4": {
        "App\\": "app/"
    },
    "files": [
        "app/MyCustomStuff/MyGlobalTools.php" 
    ]
},

after you change the autoload. Then run composer dump-autoload

Then in controller, just call the global function (without need to import), like:

public function index() {
  $res = sayHi();
}

2. Or Normal Class

App/MyCustomStuff/MyClass.php

<?php
namespace App\MyCustomStuff;
class MyClass {
    function sayhi() {
        echo 'hi';
    }
}

?>

In your controller

<?php

namespace App\Http\Controllers;

use App\MyCustomStuff\MyClass;

class TasksController extends Controller {

  public function index() {

    $myfunctions = new MyClass();
    $res = $myfunctions->sayhi();

  }

}

Solution 2:[2]

create a directory say "Helpers" inside App/Http

create one class inside Helpers directory CustomAvatar.php

<?php

class CustomAvatar{
    public $default_avatar='avatar.png';

    public function make_custom_avatar(){
        // do your operation here
    }
}

?>

now if you want to use this class inside your controller :

use App\Http\Helpers\CustomAvatar;

 ...

 public function create_user(){

 $customAvatar=new CustomAvatar();
 $defaultAvatar = $customAvatar->default_avatar;

 $user=new User();
 $user->avatar=$defaultAvatar;
 $user->save();

 }

Solution 3:[3]

In Laravel Framework you can only create a controller inside the app\Http\Controller folder. If you want to create a custom class then created inside app folder.

Example:

File: app\FAReports.php

namespace App;

Class FAReports {

// DEF //

}

Solution 4:[4]

Your original code did not work because you did not namespace your helper class. It should have been:

/App/Http/Helpers/MyClasses.php

<?php

namespace App\Http\Helpers;  //this was missing in your code

class PopularFunctions {
  public function sayhi() {
    echo 'hi';
  }
}

?>

Solution 5:[5]

Make a little change in your controller

<?php

    namespace App\Http\Controllers;
    
    use App\Http\Helpers\MyClasses\PopularFunctions;
    
    class TasksController extends Controller {
     use PopularFunctions;
      public function index() {
    
        $myfunctions = new PopularFunctions();
        $myfunctions->sayhi();
    
      }
    
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Top-Master
Solution 2 Saurabh Mistry
Solution 3 Arjun Jain
Solution 4 eballeste
Solution 5 Adnan Nawaz