Home > Backend Development > PHP Tutorial > 3 ways to use self-written class libraries in Laravel

3 ways to use self-written class libraries in Laravel

小云云
Release: 2023-03-20 13:00:01
Original
1703 people have browsed it

This article mainly introduces three ways to use self-written class libraries in Laravel. This article explains three ways to add classes that can be directly instantiated, add functions that can be directly called, and add slightly complex class libraries. You need to Friends can refer to it, I hope it can help everyone.

Although Composer allows us to reuse many existing libraries (such as those in packagist.org), we may still use some packages or libraries that are not compatible with composer. In addition, in a certain project, we may also create a certain class library, and we may not intend to make it into a composer package. At this time we can use our own unique class library in the following ways.

Add classes that can be instantiated directly

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

1 .Create the class library file app/libraries/class/myClass.php
2. Write the file content

< ?php class Message {     public static function display() {
    } } ?>
Copy after login

Add the class import path in app/start/globals.php

< ?php  ClassLoader::addDirectories(array(
    app_path().&#39;/commands&#39;,     
    app_path().&#39;/controllers&#39;,     
    app_path().&#39;/models&#39;,     
    app_path().&#39;/database/seeds&#39;,     
    app_path().&#39;/libaries/class&#39;, 
    // 在这里增加
)); ?>
Copy after login

Add the autoload directory in composer.json

"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/models",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php",
        "app/libraries/class"   //在这里增加
    ]
},
Copy after login

1. Execute composer dump-autoload to create the import mapping
2. Use the class you imported to directly call Message::display()

This method is also a way to add a queue class. Many people don’t know where the queue processing class should be placed in Laravel. In fact, follow the above method to create a queues directory in the app directory, and then allow it to be instantiated directly. Can

Add functions that can be called directly

Some people like to use v() instead of var_dump(), and it is very easy to do this in Laravel

1. Create a function file app/libraries/function/helper.php
2. Write the file content

< ?php 
 function v($msg){
     var_dump($msg);
 }
 ?>
Copy after login

Add the file to the composer automatic import list

"autoload": {
   "classmap": [
       ...
   ],
   "files": [
       "app/libraries/function/helper.php"
   ],
},
Copy after login

Or display require this file in the project. Open app/start/global.php and add:

require app_path().&#39;/libraries/function/helper.php&#39;;
Copy after login


Personally, both methods are OK. If you want to control the loading time of this file, you can even add it in the filter.php file. Add the following content

App::before( function( $request ) 
{     require( "{$GLOBALS[&#39;app&#39;]
[&#39;path.base&#39;]
}
/app/libraries/function/helper.php" ); });
Copy after login

Use the function v('hello world') directly in the project;

Add a slightly more complex class library

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

Create a psr0 or psr4 standard directory structure.

Copy code The code is as follows:

libraries
     Myapp
         Search (note directory is capitalized)
             Search.php
             SearchFacade.php
             SearchServiceProvider.php
         AnotherLib
Copy after login


The namespace of the Search class in Myapp/Search/Search.php is Myapp\Search.

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"
     }
 },
Copy after login


Use new Myapp\Search\Search() in the project to instantiate a certain class

Summary

Although Laravel does not force which method is best, there are certain standards that can make the project structure clear and save a lot of communication costs when multiple people cooperate in development.

The above is the detailed content of 3 ways to use self-written class libraries in Laravel. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template