Home > PHP Framework > Laravel > Laravel extension package development steps [Summary]

Laravel extension package development steps [Summary]

藏色散人
Release: 2020-06-17 13:46:42
forward
3945 people have browsed it

The following tutorial column will summarize the laravel expansion package development steps for you. I hope it will be helpful to friends who need it!

Laravel extension package development steps [Summary]##1. Create package

 php artisan workbench vendor/package --resources
Copy after login
Note: vendor: developer name package: package name

2. Modify the authors

in composer.json in the package

"authors": [
    {
        "name": "cicl",
        "email": "test@126.com"
    }
]
Copy after login
3. Register the created packageServiceProvider

##Execute php artisan dump-autoload# in the project root directory

##Add the package to the providers array in the app/config/app.php file and add Vendor\Package\PackageServiceProvider to the providers array.<span style="font-family:&#39;Microsoft Yahei&#39;;font-size:medium;"></span>

In order to facilitate our use, we add an alias in the aliases of app/config/app.php: <span style="font-family:&#39;Microsoft Yahei&#39;;font-size:medium;"></span>'Package' => 'Vendor \Package\Facades\Notification',

Start the development server: php artisan serve, if it is started successfully, as shown: Laravel development server started on http://localhost:8000<span style="font-family:&#39;Microsoft Yahei&#39;;font-size:medium;"> , then the basic construction of the expansion package is successful. </span>

Basic extension package structure:

        /src
            /Vendor
                /Package
                    PackageServiceProvider.php
            /config
            /lang
            /migrations
            /views
        /tests
        /public
Copy after login
4. To add an independent routing file to this package, just add it in the boot function in PackageServiceProvider Add the following code and create the routes.php file in the root directory of the package


public function boot(){    
    $this->package(&#39;vendor/package&#39;);    
    include __DIR__.&#39;/../../routes.php&#39;;
}
Copy after login

The routing file looks like this:

Route::get(&#39;test&#39;,  function(){    return "this is test";});
Copy after login
Now, in Enter the address in the browser such as: http://localhost:8000/test, and "this is test" will be output.

5. Next try to use Controller

Create a new route as follows:

Route::get(&#39;testtwo&#39;,array(&#39;as&#39; => &#39;testtwo&#39;,&#39;uses&#39; => &#39;Vendor\Package\Controllers\PackageController@getTest&#39;));
Copy after login
6, Create a new RegistrationController.php file in controllers. The code is as follows:

<?php
namespace Ccl\Lenon\Controllers;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\View;

Class LenonController extends Controller {
     public function getTest() {
         return "控制器测试";
     }
}
Copy after login

When I try to open the page, it says that the controller cannot be found. Laravel does not automatically search for any directories by default. The developer needs to do this himself. Specify the storage directory of the file, but it also gives a recommended path. For example, when using artisan workbench, is the src/controllers directory generated? Ok, we can just add the automatic loading path.

7. Add "src/controllers" to the classmap field under autoload of composer.json in the root directory of the package,

such as:

 "classmap": [
       "src/migrations",
       "src/controllers"
],
Copy after login
And run php artisan dump-autoload in the project root directory and refresh the page. 8. Next we have to use the views file! According to our understanding, we only need to modify the code in the controller, as follows:

public function getRegister()    
{  
    return View::make(&#39;test&#39;); 
}
Copy after login

Create the test.blade.php file under the package src/views/ and write in the file Related page code, for example:

This is the file of the test view

Refresh the page and find that it is not successful. In Laravel, the package will not be automatically indexed. view file, so we still need to do a little work!

9. When loading the view, specify it to look for the view file in the package. Modify the code in the controller as follows:

public function getRegister()    
{ 
    return View::make(&#39;package::test&#39;);    
}
Copy after login

Refresh the page again, and the page we are looking forward to appears,

public migration

php artisan asset:publish --bench="vendor/package"
Copy after login

Create database migration

php artisan migrate:make create_users_table --bench="vendor/package"
Copy after login

执行数据库迁移

php artisan migrate --bench="vendor/package"
Copy after login

The above is the detailed content of Laravel extension package development steps [Summary]. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
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