Helper function can be implement both with pure functions and by class based functions.
Helper function can be access anywhere in your app, Event they are autoload as well via composer on every user request.
e.g dd() data dump is an common heler function that we use while debugginh in laravel.
How to implement helper functions
1 . Make a directory in app folder with helpers name.
Create a file helper.php
Write dummy code in this file
2. Now register/set the path of this file in composer.json file
add the path in autoload object like that. I define a files key and assign array to it.Array contains all helper function files path with comma separation.
3. Now run the command to add new changes to your app.
-composer dump-autoload
4. Go to web.php file add a route to test the helper function
Now you can clearly see the output.
Now let make class based helper function.
1. create a file in hlper folder and write code inside it.
2. now set it pass go to app.php inside config directory.At the end you will find "aliases" array define you class path here.
3.Now go to web.php file and access that class based helper function
Comments
Post a Comment