Where do I put my custom classes and functions in Laravel [duplicate]
Asked Answered
H

5

9

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.

Heriot answered 22/11, 2018 at 19:4 Comment(5)
Please add your folder structure also the path and code of the class you mentioned.Boong
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.Materi
Laravel comes configured to use the composer 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 fileDan
Thanks. I updated my question. Hope this clarifies.Heriot
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.Azrael
F
17

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

  }

}
Faceharden answered 24/11, 2018 at 3:18 Comment(0)
T
5

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

 }
Thenna answered 22/11, 2018 at 19:14 Comment(1)
(1/1) FatalThrowableError Class 'App\Http\Helpers\CustomAvatar' not foundCurson
C
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 //

}

Carbonation answered 22/11, 2018 at 19:9 Comment(0)
N
-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';
  }
}

?>
Narah answered 12/9, 2020 at 7:28 Comment(0)
F
-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();
    
      }
    
    }
Fulviah answered 9/10, 2021 at 4:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.