How to use class from blade template?

后端 未结 2 1012
逝去的感伤
逝去的感伤 2021-01-14 15:15

I have created class Helper with path App\\Http\\Helpers\\:



        
2条回答
  •  伪装坚强ぢ
    2021-01-14 15:38

    You can achieve this in a straight forward way by following these steps.

    Step 1

    Create your class file in your desired path (example: app/Helpers/Helper.php) and define the class

    Step 2

    Modify composer.json file by adding your file within the entry of the autoload key

    "autoload": {
        "prs-4": {
            "App\\": "app/"
        },
        "files": [
            "app/Helpers/Helper.php"
        ]
    }
    

    Note that you are not changing anything apart from including this entry: "files": ["app/Helpers/Helper.php"]. Save the file.

    Step 3

    Run this command from your root directory:

    composer dump-autoload
    

    it refreshes the autoload cache.

    Step 4

    You can now use your class from anywhere including blade

    
    

    This way, you can also create global functions as well.

提交回复
热议问题