I would like to create helper functions to avoid repeating code between views in Laravel 5:
view.blade.php
Foo Formated text: {{ fo
Here's a bash shell script I created to make Laravel 5 facades very quickly.
Run this in your Laravel 5 installation directory.
make_facade.sh -f -n ''
Example:
make_facade.sh -f helper -n 'App\MyApp'
If you run that example, it will create the directories Facades
and Providers
under 'your_laravel_installation_dir/app/MyApp'.
./app/MyApp/Facades/Helper.php
./app/MyApp/Facades/HelperFacade.php
./app/MyApp/Providers/HelperServiceProvider.php
After it is done, it will display a message similar to the following:
===========================
Finished
===========================
Add these lines to config/app.php:
----------------------------------
Providers: App\MyApp\Providers\HelperServiceProvider,
Alias: 'Helper' => 'App\MyApp\Facades\HelperFacade',
So update the Providers and Alias list in 'config/app.php'
Run composer -o dumpautoload
The "./app/MyApp/Facades/Helper.php" will originally look like this:
Now just add your methods in "./app/MyApp/Facades/Helper.php".
Here is what "./app/MyApp/Facades/Helper.php" looks like after I added a Helper function.
This function expects a pattern and can accept an optional second boolean argument.
If the current URL matches the pattern passed to it, it will output 'active' (or 'class="active"' if you add 'true' as a second argument to the function call).
I use it to highlight the menu that is active.
#!/bin/bash
display_syntax(){
echo ""
echo " The Syntax is like this:"
echo " ========================"
echo " "$(basename $0)" -f -n ''"
echo ""
echo " Example:"
echo " ========"
echo " "$(basename $0) -f test -n "'App\MyAppDirectory'"
echo ""
}
if [ $# -ne 4 ]
then
echo ""
display_syntax
exit
else
# Use > 0 to consume one or more arguments per pass in the loop (e.g.
# some arguments don't have a corresponding value to go with it such
# as in the --default example).
while [[ $# > 0 ]]
do
key="$1"
case $key in
-n|--namespace_prefix)
namespace_prefix_in="$2"
echo ""
shift # past argument
;;
-f|--facade)
facade_name_in="$2"
shift # past argument
;;
*)
# unknown option
;;
esac
shift # past argument or value
done
fi
echo Facade Name = ${facade_name_in}
echo Namespace Prefix = $(echo ${namespace_prefix_in} | sed -e 's#\\#\\\\#')
echo ""
}
function display_start_banner(){
echo '**********************************************************'
echo '* STARTING LARAVEL MAKE FACADE SCRIPT'
echo '**********************************************************'
}
# Init the Vars that I can in the beginning
function init_and_export_vars(){
echo
echo "INIT and EXPORT VARS"
echo "===================="
# Substitution Tokens:
#
# Tokens:
# {namespace_prefix}
# {namespace_prefix_lowerfirstchar}
# {facade_name_upcase}
# {facade_name_lowercase}
#
namespace_prefix=$(echo ${namespace_prefix_in} | sed -e 's#\\#\\\\#')
namespace_prefix_lowerfirstchar=$(echo ${namespace_prefix_in} | sed -e 's#\\#/#g' -e 's/^\(.\)/\l\1/g')
facade_name_upcase=$(echo ${facade_name_in} | sed -e 's/\b\(.\)/\u\1/')
facade_name_lowercase=$(echo ${facade_name_in} | awk '{print tolower($0)}')
# Filename: {facade_name_upcase}.php - SOURCE TEMPLATE
source_template=' ./${namespace_prefix_lowerfirstchar}/Facades/${facade_name_upcase}.php
}
function createServiceProviderTemplate(){
serviceProvider_template=$(echo "${serviceProvider_template}" | sed -e 's/{namespace_prefix}/'${namespace_prefix}'/g' -e 's/{facade_name_upcase}/'${facade_name_upcase}'/g' -e 's/{facade_name_lowercase}/'${facade_name_lowercase}'/g')
echo "Create ServiceProvider Template:"
echo "================================"
echo "${serviceProvider_template}"
echo ""
echo "${serviceProvider_template}" > ./${namespace_prefix_lowerfirstchar}/Providers/${facade_name_upcase}ServiceProvider.php
}
function createFacadeTemplate(){
facade_template=$(echo "${facade_template}" | sed -e 's/{namespace_prefix}/'${namespace_prefix}'/g' -e 's/{facade_name_upcase}/'${facade_name_upcase}'/g' -e 's/{facade_name_lowercase}/'${facade_name_lowercase}'/g')
echo "Create Facade Template:"
echo "======================="
echo "${facade_template}"
echo ""
echo "${facade_template}" > ./${namespace_prefix_lowerfirstchar}/Facades/${facade_name_upcase}Facade.php
}
function serviceProviderPrompt(){
echo "Providers: ${namespace_prefix_in}\Providers\\${facade_name_upcase}ServiceProvider,"
}
function aliasPrompt(){
echo "Alias: '"${facade_name_upcase}"' => '"${namespace_prefix_in}"\Facades\\${facade_name_upcase}Facade',"
}
#
# END FUNCTION DECLARATIONS
#
###########################
## START RUNNING SCRIPT ##
###########################
display_start_banner
init_and_export_vars
makeDirectories
checkDirectoryExists
echo ""
createSourceTemplate
createServiceProviderTemplate
createFacadeTemplate
echo ""
echo "==========================="
echo " Finished TEST"
echo "==========================="
echo ""
echo "Add these lines to config/app.php:"
echo "----------------------------------"
serviceProviderPrompt
aliasPrompt
echo ""