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
- Create a class library file
app/libraries/class/myClass.php
- Write File Content
<?phpclass Message { public static function display() { }}?>
- In
app/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);?>
- In
composer.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]},
- Run
composer dump-autoloadTo create an import ing
- Direct call using self-imported classes
Message::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.
- Create a function File
app/libraries/function/helper.php
- Write File Content
<?php function v($msg){ var_dump($msg);}?>
- 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" );});
- Use functions directly in the project
v('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.
- 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.
- 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 in projects
new 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
- Adding new classes or library to laravel 4
- How to autoload 'libraries' in laravel 4?
- What are the best practices and best places for laravel 4 helpers or basic functions?
This article permanently updates the link address: