8

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.

Topher
  • 141
  • 1
  • 2
  • 9
  • Please add your folder structure also the path and code of the class you mentioned. – Kenny Horna Nov 22 '18 at 19:06
  • 2
    You should be able to put a new class virtually anywhere in the `app` directory. There's no real restrictions. If you post your code and structure etc. we might be able to help you pinpoint the problem. – Jonathon Nov 22 '18 at 19:11
  • Laravel comes configured to use the composer [PSR-4](https://www.php-fig.org/psr/psr-4/) autoloader and the root namespace is `App` and is under the `app` folder so as long as you follow the standard and place your files under `app` they should be picked up. If not try `composer dump-autoload` to re-create the autoload file – apokryfos Nov 22 '18 at 20:30
  • Thanks. I updated my question. Hope this clarifies. – Topher Nov 25 '18 at 02:50
  • Don't put it in the app/Http directory. What if you want to use the class for a console job? Then the Http directory doesn't make sense. – PhillipMcCubbin Sep 29 '21 at 20:41

5 Answers5

13

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();

  }

}
Top-Master
  • 7,611
  • 5
  • 39
  • 71
Kelvin
  • 1,004
  • 1
  • 10
  • 32
6

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();

 }
Saurabh Mistry
  • 12,833
  • 5
  • 50
  • 71
1

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 //

}

Arjun Jain
  • 401
  • 1
  • 8
  • 20
-1

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';
  }
}

?>
eballeste
  • 712
  • 1
  • 11
  • 23
-1

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();
    
      }
    
    }
Adnan Nawaz
  • 79
  • 1
  • 8