Anansewaa
laravel framework

Custom classes in laravel 5.2, the easy way

“Love beautiful code? We do too. The PHP Framework For Web Artisans “- well that is all that laravel.comcaptured about their intuitive and comprehensive framework on their website.  If you are a framework fanatic, devotee or even a bigot then I do not have to talk much about it, all the same if you are not then the following result from a simple Google search should be helpful.

In computer systems, a framework is often a layered structure indicating what kind of programs can or should be built and how they would interrelate. Some computer system frameworks also include actual programs, specify programming interfaces, or offer programming tools for using the frameworks – WhatIs.com. If the above definition did not leave you more confused than before than we good, all the same the famous Wikipedia.com provides a simpler definition. A software framework is an abstraction in which software providing generic functionality can be selectively changed by additional user-written code, thus providing application-specific software, simply put a reusable set of libraries or classes for a software system.

Back to Laravel, it is a free, open-source PHP web framework, created by Taylor Otwell and intended for the development of web applications following the model–view–controller (MVC) architectural pattern. Some of the features of Laravel are a modular packaging system with a dedicated dependency manager, different ways for accessing relational databases, utilities that aid in application deployment and maintenance, and its orientation toward syntactic sugar. As of March 2015, Laravel is regarded as one of the most popular PHP frameworks, together with Symfony2, Nette, CodeIgniter, Yii2 and other frameworks. Laravel of the writing of this article has a version number of 5.2 since it’s candidate release on June 9, 2011.

Let’s hold off on the history just a little bit, thus an article for another day and once again get back to the topic under consideration. A simple Google search on the topic would yield tons of result of diverse methods or ways to achieve the same goal, but the enlisted method in my candid opinion is the simplest way.  If you have done some development in PHP then you can greatly appreciate the need and use of custom functions or methods as they are called in the MVC world in a development process.

The following steps are on the assumption that reader:

  1. Has working knowledge of a unix or Linux system and can run simple commads and create files.
  2. Has a working copy of a Laravel 5.2 application on a linux system, with the following application structure

Laravel 5.2 application
Step 1
Create a Helpers folder under the app directory of the Laravel application and then create a Helper.php file in the Helpers folder. The final path structure of the file must be as follows  app\Helpers\Helper.php

The Helper.php file created is the file to house all you custom function.  Define a Helper class and place all the methods (custom functions ) in this file

Step 2
Create an alias for the Helper class defined in the Helper.php. To do this navigate to the config folder in the Laravel application and edit the  app.php file . Navigate to the section that reads ‘aliases’ => [ …]  and add the following
‘Helper’ => App\Helpers\Helper::class, Save and exit the file after this.
Step 3
Use it in your Blade template. After the first two steps you now use your custom method (functions) in Laravel’s Blade files
What of if I want to use it in a controller what do I do? With the completions of the first two processes the methods can be used almost anywhere in your Laravel application. To use it in a controller, you need to import the namespace for the class in the controller before using the static method access your static methods.

Allan Barku

People are often unreasonable and self-centered. Forgive them anyway. If you are kind, people may accuse you of ulterior motives. Be kind anyway. If you are honest, people may cheat you. Be honest anyway. If you find happiness, people may be jealous. Be happy anyway.

Add comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow us

Don't be shy, get in touch. We love meeting interesting people and making new friends.

Most discussed

%d bloggers like this: