Laravel helpers (composer reload)

Add “files” in composer.json:

{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": ">=5.6.4",
        "laravel/framework": "5.3.*"
    },
    "require-dev": {
        "fzaninotto/faker": "~1.4",
        "mockery/mockery": "0.9.*",
        "phpunit/phpunit": "~5.0",
        "symfony/css-selector": "3.1.*",
        "symfony/dom-crawler": "3.1.*"
    },
    "autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "App\\": "app/"
        }, 
        "files":[
            "app/Helpers/helper.php"
        ]
    },
    "autoload-dev": {
        "classmap": [
            "tests/TestCase.php"
        ]
    },
    "scripts": {
        "post-root-package-install": [
            "php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "php artisan key:generate"
        ],
        "post-install-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postInstall",
            "php artisan optimize"
        ],
        "post-update-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postUpdate",
            "php artisan optimize"
        ]
    },
    "config": {
        "preferred-install": "dist",
        "sort-packages": true
    }
}

Update autoload.php

$ composer dump-autoload

Custom Helper

<?php
use Carbon\Carbon;


if(!function_exists('logg'))
{
    function logg($value, $stop=true)
    {
        if(is_array($value) == true || is_object($value) == true)
        {
            print "<pre>";
            print_r($value);
            print "<pre>";
        }
        else
        {
            echo var_dump($value);
        }

        if($stop == true)
        {
            exit();
        }
    }
}

if(!function_exists('val')){
    function val($object, $key, $default_value=''){
        if(isset($object) == false){
            return $default_value;
        }

        if(is_object($object) == true){
            if(isset($object->{$key}) == true){
                return $object->{$key};
            }else{
                return $default_value;
            }
        }

        if(is_array($object) == true){
            if(isset($object[$key]) == true){
                return $object[$key];
            }else{
                return $default_value;
            }
        }
    }
}

if ( ! function_exists('show_alert'))
{
    function show_alert($msg, $url='') 
    {
        echo '<html><head><meta charset="UTF-8">';
        echo '<script>alert("' . $msg . '");';

        if($url != '' && strlen($url) > 0){
            echo 'window.location.href = "' . $url . '";';
        }else{
            echo 'window.history.back();';
        }

        echo '</script>';
        echo '</head><body></body></html>';
        exit();
    }
}

if ( ! function_exists('img_url'))
{
    function img_url($file) 
    {
        $file = str_replace('\\', '/', $file);
        $url = BASE_URL . 'storage/' . $file;
        return $url;
    }
}

if ( ! function_exists('std_date'))
{
    function std_date($date) 
    {
        return Carbon::parse($date)->format('Y/m/d');
    }
}

if(!function_exists('go')){
    function go($url){
        header('location:' . $url);
        exit();
    }
}

if(!function_exists('is_collection')){
    function is_collection($data){
        if($data instanceof Illuminate\Database\Eloquent\Collection){
            return true;
        }else{
            return false;
        }
    }
}

 

Reference:

  1. https://kejyuntw.gitbooks.io/laravel-5-learning-notes/services/helpers/services-custom-helpers-README.html