Where do I put my custom classes and functions in Laravel












0














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 AppHttpControllers;

use AppHttpHelpersMyClasses;

class TasksController extends Controller {

public function index() {

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

}

}


This returns: Class 'AppHttpControllersPopularFunctions' not found.










share|improve this question
























  • Please add your folder structure also the path and code of the class you mentioned.
    – HCK
    Nov 22 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 at 19:11










  • 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 file
    – apokryfos
    Nov 22 at 20:30










  • Thanks. I updated my question. Hope this clarifies.
    – Topher
    Nov 25 at 2:50
















0














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 AppHttpControllers;

use AppHttpHelpersMyClasses;

class TasksController extends Controller {

public function index() {

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

}

}


This returns: Class 'AppHttpControllersPopularFunctions' not found.










share|improve this question
























  • Please add your folder structure also the path and code of the class you mentioned.
    – HCK
    Nov 22 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 at 19:11










  • 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 file
    – apokryfos
    Nov 22 at 20:30










  • Thanks. I updated my question. Hope this clarifies.
    – Topher
    Nov 25 at 2:50














0












0








0







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 AppHttpControllers;

use AppHttpHelpersMyClasses;

class TasksController extends Controller {

public function index() {

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

}

}


This returns: Class 'AppHttpControllersPopularFunctions' not found.










share|improve this question















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 AppHttpControllers;

use AppHttpHelpersMyClasses;

class TasksController extends Controller {

public function index() {

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

}

}


This returns: Class 'AppHttpControllersPopularFunctions' not found.







php laravel function class namespaces






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 at 2:55

























asked Nov 22 at 19:04









Topher

54




54












  • Please add your folder structure also the path and code of the class you mentioned.
    – HCK
    Nov 22 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 at 19:11










  • 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 file
    – apokryfos
    Nov 22 at 20:30










  • Thanks. I updated my question. Hope this clarifies.
    – Topher
    Nov 25 at 2:50


















  • Please add your folder structure also the path and code of the class you mentioned.
    – HCK
    Nov 22 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 at 19:11










  • 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 file
    – apokryfos
    Nov 22 at 20:30










  • Thanks. I updated my question. Hope this clarifies.
    – Topher
    Nov 25 at 2:50
















Please add your folder structure also the path and code of the class you mentioned.
– HCK
Nov 22 at 19:06




Please add your folder structure also the path and code of the class you mentioned.
– HCK
Nov 22 at 19:06




2




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 at 19:11




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 at 19:11












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 file
– apokryfos
Nov 22 at 20:30




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 file
– apokryfos
Nov 22 at 20:30












Thanks. I updated my question. Hope this clarifies.
– Topher
Nov 25 at 2:50




Thanks. I updated my question. Hope this clarifies.
– Topher
Nov 25 at 2:50












3 Answers
3






active

oldest

votes


















0














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



Example:



File: appFAReports.php




namespace App;



Class FAReports {



// DEF //



}







share|improve this answer





























    0














    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 AppHttpHelpersCustomAvatar;

    ...

    public function create_user(){

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

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

    }





    share|improve this answer





























      0














      1. Through Composer



      App/Http/Helpers/MyClasses.php



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


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



      "files": [
      "app/Http/Helpers/MyClasses.php"
      ]


      so the structure will be



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


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



      then in controller just call the function in your helpers



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


      2. Class



      App/Http/Helpers/MyClasses.php



      <?php
      namespace AppHttpHelpers;
      class MyClassess {
      function sayhi() {
      echo 'hi';
      }
      }

      ?>


      In your controller



      <?php

      namespace AppHttpControllers;

      use AppHttpHelpersMyClasses;

      class TasksController extends Controller {

      public function index() {

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

      }

      }





      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436841%2fwhere-do-i-put-my-custom-classes-and-functions-in-laravel%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














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



        Example:



        File: appFAReports.php




        namespace App;



        Class FAReports {



        // DEF //



        }







        share|improve this answer


























          0














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



          Example:



          File: appFAReports.php




          namespace App;



          Class FAReports {



          // DEF //



          }







          share|improve this answer
























            0












            0








            0






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



            Example:



            File: appFAReports.php




            namespace App;



            Class FAReports {



            // DEF //



            }







            share|improve this answer












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



            Example:



            File: appFAReports.php




            namespace App;



            Class FAReports {



            // DEF //



            }








            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 22 at 19:09









            Arjun Jain

            87616




            87616

























                0














                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 AppHttpHelpersCustomAvatar;

                ...

                public function create_user(){

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

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

                }





                share|improve this answer


























                  0














                  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 AppHttpHelpersCustomAvatar;

                  ...

                  public function create_user(){

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

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

                  }





                  share|improve this answer
























                    0












                    0








                    0






                    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 AppHttpHelpersCustomAvatar;

                    ...

                    public function create_user(){

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

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

                    }





                    share|improve this answer












                    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 AppHttpHelpersCustomAvatar;

                    ...

                    public function create_user(){

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

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

                    }






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 22 at 19:14









                    Saurabh Mistry

                    3,1971726




                    3,1971726























                        0














                        1. Through Composer



                        App/Http/Helpers/MyClasses.php



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


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



                        "files": [
                        "app/Http/Helpers/MyClasses.php"
                        ]


                        so the structure will be



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


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



                        then in controller just call the function in your helpers



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


                        2. Class



                        App/Http/Helpers/MyClasses.php



                        <?php
                        namespace AppHttpHelpers;
                        class MyClassess {
                        function sayhi() {
                        echo 'hi';
                        }
                        }

                        ?>


                        In your controller



                        <?php

                        namespace AppHttpControllers;

                        use AppHttpHelpersMyClasses;

                        class TasksController extends Controller {

                        public function index() {

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

                        }

                        }





                        share|improve this answer




























                          0














                          1. Through Composer



                          App/Http/Helpers/MyClasses.php



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


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



                          "files": [
                          "app/Http/Helpers/MyClasses.php"
                          ]


                          so the structure will be



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


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



                          then in controller just call the function in your helpers



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


                          2. Class



                          App/Http/Helpers/MyClasses.php



                          <?php
                          namespace AppHttpHelpers;
                          class MyClassess {
                          function sayhi() {
                          echo 'hi';
                          }
                          }

                          ?>


                          In your controller



                          <?php

                          namespace AppHttpControllers;

                          use AppHttpHelpersMyClasses;

                          class TasksController extends Controller {

                          public function index() {

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

                          }

                          }





                          share|improve this answer


























                            0












                            0








                            0






                            1. Through Composer



                            App/Http/Helpers/MyClasses.php



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


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



                            "files": [
                            "app/Http/Helpers/MyClasses.php"
                            ]


                            so the structure will be



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


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



                            then in controller just call the function in your helpers



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


                            2. Class



                            App/Http/Helpers/MyClasses.php



                            <?php
                            namespace AppHttpHelpers;
                            class MyClassess {
                            function sayhi() {
                            echo 'hi';
                            }
                            }

                            ?>


                            In your controller



                            <?php

                            namespace AppHttpControllers;

                            use AppHttpHelpersMyClasses;

                            class TasksController extends Controller {

                            public function index() {

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

                            }

                            }





                            share|improve this answer














                            1. Through Composer



                            App/Http/Helpers/MyClasses.php



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


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



                            "files": [
                            "app/Http/Helpers/MyClasses.php"
                            ]


                            so the structure will be



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


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



                            then in controller just call the function in your helpers



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


                            2. Class



                            App/Http/Helpers/MyClasses.php



                            <?php
                            namespace AppHttpHelpers;
                            class MyClassess {
                            function sayhi() {
                            echo 'hi';
                            }
                            }

                            ?>


                            In your controller



                            <?php

                            namespace AppHttpControllers;

                            use AppHttpHelpersMyClasses;

                            class TasksController extends Controller {

                            public function index() {

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

                            }

                            }






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 24 at 3:25

























                            answered Nov 24 at 3:18









                            Kelvin

                            362224




                            362224






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436841%2fwhere-do-i-put-my-custom-classes-and-functions-in-laravel%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                                Calculate evaluation metrics using cross_val_predict sklearn

                                Insert data from modal to MySQL (multiple modal on website)