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 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
<span style="font-size: 16px;"><?php<br/>class Message {<br/> public static function display() {<br/> }<br/>}<br/>?><br/></span>
in app Add the class import path to /start/globals.php
<span style="font-size: 16px;"><?php <br/>ClassLoader::addDirectories(array(<br/> app_path().'/commands',<br/> app_path().'/controllers',<br/> app_path().'/models',<br/> app_path().'/database/seeds',<br/> app_path().'/libaries/class', // 在这里增加<br/>));<br/>?><br/></span>
Add the autoload directory to composer.json
<span style="font-size: 16px;">"autoload": {<br/> "classmap": [<br/> "app/commands",<br/> "app/controllers",<br/> "app/models",<br/> "app/database/migrations",<br/> "app/database/seeds",<br/> "app/tests/TestCase.php",<br/> "app/libraries/class" //在这里增加<br/> ]<br/>},<br/></span>
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 in Laravel should be placed. In fact, according to the above method, create a queues directory in the app directory, and then allow it to be instantiated directly.
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
<span style="font-size: 16px;"><?php <br/>function v($msg){<br/> var_dump($msg);<br/>}<br/>?><br/></span>
Add the file to the composer automatic import list
<span style="font-size: 16px;">"autoload": {<br/> "classmap": [<br/> ...<br/> ],<br/> "files": [<br/> "app/libraries/function/helper.php"<br/> ],<br/>},<br/></span>
Or display require this file in the project. Open app/start/global.php and add at the end:
<span style="font-size: 16px;">require app_path().'/libraries/function/helper.php';<br/></span>
Personally, I think both methods are OK. If you want to control the loading time of this file, you can even add it in filter. Add the following content to the php file
<span style="font-size: 16px;">App::before( function( $request ) {<br/> require( "{$GLOBALS['app']['path.base']}/app/libraries/function/helper.php" );<br/>});<br/></span>
Use the function v('hello world') directly in the project;
Add a slightly more complex class library
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.
libraries Myapp Search (note directory is capitalized) Search.php SearchFacade.php SearchServiceProvider.php AnotherLib
The namespace of the Search class in Myapp/Search/Search.php is MyappSearch.
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" } },
Use new MyappSearchSearch() in the project to instantiate a certain class
Summary
Although Laravel does not force which The method is the best, but there are certain standards that can make the project structure clear and save a lot of communication costs when multiple people cooperate in development.
For more related articles on 3 ways to use self-written class libraries in Laravel, please pay attention to the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.