Three methods of using your own class library in Laravel

Source: Internet
Author: User

Three methods of using your own class library in Laravel

Although Composer allows us to reuse many existing class libraries (such as packagist.org), we may still use some packages or class libraries that are not compatible with composer. In addition, a certain type of library may be created in a project, and it may not be intended to become a composer package. In this case, we can use our own class library in the following ways.

Add classes that can be directly instantiated

Some classes that need to be used directly in the project can be added to Laravel in the following ways

  1. Create a class library fileapp/libraries/class/myClass.php
  2. Write File Content
<?phpclass Message {    public static function display() {    }}?>
  1. Inapp/start/globals.phpAdd the class import path
<? Php ClassLoader: addDirectories (array (app_path (). '/commands', app_path (). '/controllers', app_path (). '/models', app_path (). '/database/seeds', app_path (). '/libaries/class', // added here);?>
  1. Incomposer.jsonAdd the autoload directory
"Autoload": {"classmap": ["app/commands", "app/controllers", "app/models", "app/database/migrations ", "app/database/seeds", "app/tests/TestCase. php "," app/libraries/class "// Add here]},
  1. Runcomposer dump-autoloadTo create an import ing
  2. Direct call using self-imported classesMessage::display()You can.

This method also adds the queue class. Many people do not know where the queue processing class should be stored in Laravel. In fact, according to the above methodappCreatequeuesDirectory, and then it can be directly instantiated.

Add functions that can be called directly

Some people prefer to usev()To replacevar_dump()It is also very easy to do this in Laravel.

  1. Create a function Fileapp/libraries/function/helper.php
  2. Write File Content
<?php function v($msg){    var_dump($msg);}?>
  1. Add the file to the composer automatic import list.
"autoload": {   "classmap": [       ...   ],   "files": [       "app/libraries/function/helper.php"   ],},

Or displayrequireThis file. Openapp/start/global.php, Add at the end:

require app_path().'/libraries/function/helper.php';

I personally feel that both methods are OK. If you want to control the file loading time, you can evenfilter.phpAdd the following content to the file:

App::before( function( $request ) {    require( "{$GLOBALS['app']['path.base']}/app/libraries/function/helper.php" );});
  1. Use functions directly in the projectv('hello world');
Add a slightly complex class library

Sometimes a class library is not just a file, so the following method is more suitable for class libraries with multiple structures of multiple files.

  1. Create a standard directory structure of psr0 or psr4.
libraries    Myapp        Search (note directory is capitalized)            Search.php            SearchFacade.php            SearchServiceProvider.php        AnotherLib

Myapp/Search/Search.phpMediumSearchThe namespace of the class isMyapp\Search.

  1. Modify autoload in composer
"autoload": {    "classmap": [        "app/commands",        "app/controllers",        "app/models",        "app/libraries",        "app/database/migrations",        "app/database/seeds",        "app/tests/TestCase.php"    ]    ,    "psr-0": {         "Myapp": "app/libraries"    }},
  1. Use in projectsnew Myapp\Search\Search()To instantiate a class
Summary

Although Laravel does not force the best method, there are some standards that can make the project structure clear, and many people in Cooperative Development save a lot of communication costs.

Reference
  1. Adding new classes or library to laravel 4
  2. How to autoload 'libraries' in laravel 4?
  3. What are the best practices and best places for laravel 4 helpers or basic functions?

This article permanently updates the link address:

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.