Custom classes in Laravel 5.2

The PHP Framework For Web Artisans

“Love beautiful code? We do too. The PHP Framework For Web Artisans “- well that is all that laravel.com captured 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 this article, we will be focusing on creating custom classes in laravel 5.2 application.

What is a framework?

In computer systems, a framework is a layered structure indicating what kind of programs to built and how they 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.

PHP web framework

Back to Laravel, it is a free, open-source PHP web framework, created by Taylor Otwell. It is 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 will 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 the reader:

  1. Has a working knowledge of a Unix or Linux system and can run simple commands 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: Creating a Helpers folder and a Helper class

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 your custom function.  Define a Helper class and place all the methods (custom functions ) in this file

<?php // Code within app\Helpers\Helper.php

namespace App\Helpers;

class Helper
{	
        //simple class to capitalize
	public static function uppercase($string)
	{
		return strtoupper($string); 
	}
}

Step 2: Define an alias for the Helper class

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.

<?php // Code within config/app.php

'aliases' => [
        ....
        'Helper' => App\Helpers\Helper::class,
        ....
       ]

Step 3: Blade Template

Use it in your Blade template. After the first two steps, you now use your custom method (functions) in Laravel’s Blade files

<!-- Code within resources/views/welcome.blade.php -->
@extends('layouts.app')
@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-10 col-md-offset-1">
            <div class="panel panel-default">
                <div class="panel-heading">Welcome</div>

                <div class="panel-body">
                    Your Application's Landing Page.
                    {!! Helper::Uppercase('this is how to use autoloading correctly!!') !!}
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

What of if I want to use it in a controller what do I do? With the completion of the first two processes, the methods will be useful 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.

<?php // Code within app/Http/Controllers/SomeController.php

namespace App\Http\Controllers;

use App\Helpers\Helper;

class SomeController extends Controller
{

    public function __construct()
    {
        Helper::uppercase('now i\'m using my helper class in a controller!!');
    }
    ...
Comments to: Custom classes in laravel 5.2, the easy way

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.

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

Login

Welcome to Typer

Brief and amiable onboarding is the first thing a new user sees in the theme.
Join Typer
Registration is closed.
%d bloggers like this: