Extension Function in Laravel Framework Extension Method of Custom Class

  • 2021-07-18 07:25:41
  • OfStack

1. Extend your classes

Create a directory under app/libraries\ class

Then myTest. php class name format hump myTest


<?php
class myTest
{
public  function test()
{
return '1asdasd111';
}
}

In app/start/global. php


ClassLoader::addDirectories(array(
app_path().'/commands',
app_path().'/controllers',
app_path().'/models',
app_path().'/database/seeds',
app_path().'/libraries/class', // Increase this 1 Segment
));

Load with make

<?php
class HomeController extends BaseController {
    protected $layout = 'layouts.main';
public function index()
{
$a = App::make('mytest'); // Usage
echo $a->test();
}
}

2. Extend your functions

Create a directory under app/libraries\ function

Establish helper. php

Function format, as follows, use function_exists to prevent duplicate names with the system


if (! function_exists('test2'))
{
function test2()
{
echo 2222222222222222;
}
}

Method 1:

In app/filters. php


App::before(function($request)
{
require app_path().'/libraries/function/helper.php'; // Load Custom function
});

Method 2:

In app/bootstrap/autolad. php


require __DIR__.'/../app/functions.php'; // Introducing custom function library

I feel that method 1 will be better.


Related articles: