title | isChild | anchor |
---|---|---|
Functions |
true |
functions |
Laravel comes with a lot of useful helper functions, but you can also define your own helper functions, given the following conditions:
Good
project_folder/app/helper.php
project_folder/app/Http/helper.php
Bad
project_folder/functions.php
Good
// file composer.json
...
"autoload": {
"files": [
"app/helpers.php"
],
...
Bad
// file app/Http/Controllers/HomeController.php
class HomeController.php
{
function index(){
require_once(app_path("helpers.php"));
}
}
Good
if (! function_exists('my_custom_helper')) {
function my_custom_helper($key, $default = null) {
// ...
}
}
Bad
function my_custom_helper($key, $default = null) {
// ...
}
- If the function length exceeds 25 lines, you SHOULD break it down to multiple functions
- Each function SHOULD have a Unit Test associated with it