Skip to content

Instantly share code, notes, and snippets.

@jakewtaylor
Last active November 6, 2017 12:08
Show Gist options
  • Save jakewtaylor/92b3e019c8ac3644aeccbed0b595bf6e to your computer and use it in GitHub Desktop.
Save jakewtaylor/92b3e019c8ac3644aeccbed0b595bf6e to your computer and use it in GitHub Desktop.
Autoload example for Salem

Composer.json

You'll need to add the following to composer.json (some of it is already in there, you need to add the files key and contents.

The app/Support/helpers.php is just an example, and where I would typically put helper functions, but you can put them wherever you like. If you do change it, just change all occurences of app/Support/helpers.php to the path you chose.

"autoload": {
"classmap": [
"..."
],
"psr-4": {
"App\\": "app/"
},
// THIS BIT HERE IS WHAT YOU'RE CHANGING
"files": [
"app/Support/helpers.php"
]
// NO MORE AFTER HERE
}
/*
* You need to just add the "files": [...] part,
* everything else should be in the for you
* (or look vaguley similary, you can work it out!)
*/
<?php
/*
* You can then add any other helper files you might
* need in this file.
* Try to keep all helper functions in the helper.php file, though.
*/
// Checks there isn't already a function called 'title'
if (!function_exists('title')) {
// Creates a function that takes a string '$title' and returns a new string with the env('APP_NAME') appended
function title (string $title) : string {
// Get the app name
$appName = env('APP_NAME');
// return the given $title and the $appName, separated by a '-' (you can change this to a '|' or whatever you want
return "$title - $appName";
}
/*
* You can repeat this format as much as you want here,
* create as many globals as you need, although you
* shouldn't need too many.
* Make sure to do the `if (!function_exists('...'))` part
* so you don't override any Laravel functions, that could break the system :P
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment