_php instance of extended function and extended custom class in Laravel framework

Source: Internet
Author: User
Tags php class

First, expand your own class

Create a directory under app/libraries\class

Then mytest.php class name format hump myTest

Copy Code code as follows:

<?php
Class MyTest
{
Public Function test ()
{
Return ' 1asdasd111 ';
}
}

In app/start/global.php

Copy Code code as follows:

Classloader::adddirectories Array (
App_path (). ' /commands ',
App_path (). ' /controllers ',
App_path (). ' /models ',
App_path (). ' /database/seeds ',
App_path (). ' /libraries/class ',//Increase this section
));

Load with Make
Copy Code code as follows:

<?php
Class HomeController extends Basecontroller {
protected $layout = ' layouts.main ';
Public Function Index ()
{
$a = App::make (' mytest '); Usage
echo $a->test ();
}
}

Second, expand their own functions

Create a directory under app/libraries\function

Establish helper.php

function format, such as function_exists, to prevent duplicate system name

Copy Code code as follows:

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

Method One:

In app/filters.php

Copy Code code as follows:

App::before (function ($request)
{
Require App_path (). ' /libraries/function/helper.php '; Load a custom function
});

Method Two:

In app/bootstrap/autolad.php

Copy Code code as follows:

Require __dir__. ' /.. /app/functions.php '; Introducing a Custom Function library

I feel the method will be better in a minute.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.