Best Practices for Laravel 4 Helpers and Basic Functions? - laravel

I'm trying to understand the best place to put a global function in Laravel 4. For example, date formatting. I don't think making a facade is worth it as facades are too modular. I've read articles about creating a library folder and storing classes there but that also seems like a lot for a simple function. Shouldn't a 'tool' like this be available in Blade templates?
What are the best practices for something like this and how do I make it available to Blade templates?

The ugly, lazy and awful way: At the end of bootstrap/start.php , add an include('tools.php') and place your function in that new file.
The clean way: Create a library. That way it'll be autoloaded ONLY when you actually use it.
Create a libraries folder inside your app folder
Create your library file, create a class in it, and add static functions to it
Option 1: Edit start/global.php to add app_path().'/libraries' to the ClassLoader::addDirectories( array.
Option 2: Edit composer.json to add "app/libraries" to the autoload array. Run composer dump-autoload
Call your class and static functions from your views.
About your options, quoted from the global.php file
In addition to using Composer, you may use the Laravel class loader to
load your controllers and models. This is useful for keeping all of
your classes in the "global" namespace without Composer updating.
You can combine both options, where the Laravel class loader will automatically search for classes in the registered directories (Option 1, easier) and Composer will keep record of all the classes but only after you update it (Option 2, might improve performance).

My way of doing this is to create a new folder in the /app directory in the root of your Laravel 4 project. Then add this folder to the first array of the /app/start/global.php file like so:
<?php
ClassLoader::addDirectories(array(
app_path().'/commands',
app_path().'/controllers',
app_path().'/models',
app_path().'/database/seeds',
app_path().'/classes', // This line is the one I've added.
));
As long as the folder structure within the new /app/classes folder follows your namespacing convention. Laravel 4 will autoload all the classes/files within this folder. This way there's no need to dig into any composer files or run composer command.
Not sure if this is best practice but it certainly works.
If you created a simple file called /app/classes/Helpers/Helper.php such as this:
<?php namespace Helpers;
class Helper {
public static function helloWorld()
{
return 'Hello World';
}
}
All you would need to do is call Helpers\Helper::helloWorld();
You could also alias this helper class in your /app/config/app.php file. Just add something like this to the end of the aliases array:
'Helper' => 'Helpers\Helper'

Laravel's helpers.php method is to add it to your "files" in composer.json (https://github.com/laravel/framework/blob/master/composer.json):
"autoload": {
"classmap": [
...
],
"files": [
"app/libraries/helpers.php"
],
},
What I do is to create small classes (a few methods per class, one line per method, everything extended from something and DRY, that's my goal),
class ExtendedCarbon extends Carbon\Carbon {
public function formatDDMMAAAA($date)
{
/// format and return
}
}
save them to them in app/libraries and add to composer.json:
"autoload": {
"classmap": [
...
"app/libraries",
...
],
},
Execute
composer dump
And then just use them wherever you need
$formatted = (new ExtendedCarbon)->formatDDMMAAAA($date);
Watch this video about refactoring: http://www.youtube.com/watch?v=DC-pQPq0acs
By the way, I'm kind of sure it was just an example, but you might not need a helper to format dates, since all dates in Laravel are instances of Carbon (https://github.com/briannesbitt/Carbon) and it has loads of methods to format date and time.

You can also use View::share() together with closures to achieve this - I just posted about this: http://www.develophp.org/2014/07/laravel-4-blade-helper-functions/
Added benefit: You don't need to create an extra class and also keep the global namespace clean.

Related

Two models, two fields, return preferred if present

Been struggling with how to do this the most optimized way possible...
I have two models: Catalog and Application.
Catalog has a field called name.
Application has a field called name.
Both have a relationship with each other.
I am struggling to find a way to create a function i could use across my Laravel application which i would pass application.id to it and it would return a $app->name value based on the following logic:
if $application->name exists, use this value as the $app->name for the $application object
otherwise, get the $catalog->name value and use it as the $app->name
Note that I would like to create a component #application() where i can simply pass the $application->id and build the display logic (theming/styling) into it.
Since i display this $app->name in many places, i would like to make it as lightweight as possible to avoid unnecessary queries.
I hope this makes sense! There are probably so many ways to go with it, i am lost at figuring out the way way to do this :(
I'm not completely sure to understand your model/DB design, but you could use a custom Helper to use that function through the whole app.
For that, you can create a simple PHP class Helper.php file in app/Http/Helpers folder or whatever location you want. Something like:
<?php
use App\Catalog;
use App\Application;
if (! function_exists('getAppName')) {
function getAppName($id){
// Do your logic here to return the name
$catalog = Catalog::find($id);
return $catalog->name;
}
}
?>
Then in any controller or view, you just do
getAppName($application->id)
Do no forget to add your helpers file to the composer autoload. So in composer.json in Laravel's root folder, add the helper path to the autoload array:
"files": [
"app/Http/Helpers/helpers.php"
],
Last but not least, run the following command:
composer dump-autoload
Please note that function logic is just for sample purposes since I don't know your model structure.
In my opinion, I care about the database cost.
Use ternary expression will be elegant. But it took two times IO costs from database if application name is empty.
$app_name = Application::find($id)->name;
$app_name = empty($app_name) ? Catalog::where('application_id', $id)->first()->name;
And this will more complicated, but the catalog_query only execute when application.name is empty, it execute in database and the result is taken out only once;
And Database will only find the name from one table or two table.
Something like this:
$catalog_query = Catalog::where('catalogs.application_id', $id)->select('catalogs.name')->groupBy('catalogs.name');
// if catalogs and applications relationship is 1:1, use ->limit(1) or remove groupBy('name') is better.
Application::where("applications.id", $id)
->selectRaw("IF(application.name IS NULL OR application.name = '', (" . $catalog_query->toSql() ."), applications.name ) AS app_name")
->mergeBindings($catalog_query->getQuery())
->first()
->app_name;
Hope this will help you.

Where is the correct place to locate custom functions in Laravel?

In my projects I usually create several functions, which some of them are debug functions (I use microtimes, var_dumps, print_r, ...) to customize the data, and others are functions that end up being used very often in the project (for example functions that do certain things with session variables, that works with an object / array and return it in a certain way, etc.).
I always have doubts about where I should include these functions. So far the second type of functions (work with session variables, general scope functions that alter project variables) put them in a controller (CommonController), while the first type of functions (debug functions in the vast majority) I thought to put in a helper.
However, I still have serious doubts about whether both groups of functions should be placed on those sites.
Please, someone who can guide me a little?
Thank you!
Follow this steps:
create a helpers.php file to app folder
and this code to composer.json file :
"autoload": {
...
"files": [
"app/helpers.php"
]
},
For example :
helpers.php
function showTest(){
dd("ok");
}
UserController.php
public function index(Request $request) {
showTest();
}

Dynamically Register Commands in Laravel

I am able to register Events programmatically Illuminate\Support\Facades\Event and it's listener method. I would like to register command dynamically a similar way. Is there a way to do it in Laravel? Or what is the best way of doing in Laravel except for registering it inside app/Console/Kernel.php ?
Update
I am able to register a single class via the following code.
use Illuminate\Console\Application as Artisan;
if (app()->runningInConsole()) {
Artisan::starting(function ($artisan) use ($commandClass) {
$artisan->resolveCommands($commandClass);
});
}
If you look into your app/Console/Kernel.php you should see a statement like this:
$this->load(__DIR__.'/Commands');
This means that all command classes saved in app/Console/Commands/ will be automatically loaded and registered. Furthermore, if you create a command using artisan, Ex: php artisan make:command MyCommand, the class will be stored in app/Console/Commands/MyCommand.php.
While method provided by Pablo could be a better option for a single directory but if you have commands spread across different namespaces and directories one may end up adding multiple entries in app/Console/Kernel.php
In my use case the $commandClass is pulled from multiple xml files spread across multiple composer packages, therefore, I had to use this approach:
use Illuminate\Console\Application as Artisan;
// fetch command classes from different sources and register it here.
if (app()->runningInConsole()) {
Artisan::starting(function ($artisan) use ($commandClass) {
$artisan->resolveCommands($commandClass);
});
}

Isolate external php code in Laravel

I need to integrate Slider Revolution editor into my Laravel (5.5) app.
I've put the editor in public/revslider/ folder to be able to use the visual editor. I also created a helper class to "communicate" with it and be able to use it inside my Blade views:
namespace App\Helpers;
include( public_path('revslider/embed.php') );
class Slider{
/**
* This function is called where you want to put your slider
*/
public static function make($slider){
return \RevSliderEmbedder::putRevSlider( $slider );
}
/**
* This function is called inside <HEAD> tag to include all
* SR assets (js/css/font files)
*/
public static function head(){
return \RevSliderEmbedder::headIncludes(false);
}
}
The SR's PHP code does not use namespaces. In fact it is a strange mix of Code Igniter, Wordpress and vanilla php.
The problem is it is trying to declare a translation function __(...):
if( ! function_exists('__'))
{
function __($string = '')
{
....
}
}
and since there is already such Laravel's helper function, it does not redeclare it and tries to use Laravel's __() function. And that obviously causes errors.
I temporarily managed to fix this problem by changing the name of SR's __() function (and all references to it). But of course it is not a best way to solve this problem, since I will be unable to use SR's automatic updates or will be forced to do these changes after every update.
So my questions are:
Is there any good way of integrating such "bad" code into your project, invoking it safely without conflicts? Is there any way of isolating such code and avoid clashes? By "bad code" I mean code that does not follow strict OOP/PSR rules present in projects like Laravel.
What is the best way to include "external" PHP code? I've just used plain include() inside of my helper class' file, but is there a better/cleaner way? Like, I don't know, loading it through composer?

composer autoload files and variables

I'm creating a class that gets a random string from an array. The data will not change enough to make using a database worth it, but there are a few hundred entries so I don't really want to put the entire thing, into the class I'm creating.
Is there a way I can use composers autoload files to create a new php file, dump the array in and use it where ever I like? I've read up and to do that you apparently need to use the globals keyword which I'd rather not do.
Am I thinking too much about this? Would you just add the array to the class, or do a require etc, the functionality of the class itself is tiny.
Thanks.
Lets say you have these strings as text file. If its composer package, you can use composer scripts and generate php file from this text file during installation.
Then you can have one class with your functionality and second class containing array of items only.
Items class generation can look like (not tested):
<?php
...
$items = file_get_contents('items.txt');
$fileContent = '<?php
namespace Your\Namspace;
class Items {
public function getItems() {
return [
\'' . implode("',\n\t\t\t'", array_filter(explode("\n", $items))) . '\'
];
}
}';
file_put_contents('Items.php', $fileContent);

Resources