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

Laravel extension package development steps [Summary]

藏色散人
藏色散人forward
2020-06-17 13:42:233923browse

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
Note: vendor: developer name package: package name

2. Modify the authors

in composer.json in the package

"authors": [
    {
        "name": "cicl",
        "email": "test@126.com"
    }
]
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:'Microsoft Yahei';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:'Microsoft Yahei';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:'Microsoft Yahei';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
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('vendor/package');    
    include __DIR__.'/../../routes.php';
}

The routing file looks like this:

Route::get('test',  function(){    return "this is test";});
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('testtwo',array('as' => 'testtwo','uses' => 'Vendor\Package\Controllers\PackageController@getTest'));
6, Create a new RegistrationController.php file in controllers. The code is as follows:

9490a5f7ed41b82eff98bfd57ebb9277This is the file of the test view473f0a7621bec819994bb5020d29372a<span style="font-family:'Microsoft Yahei';font-size:large;"><strong></strong>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! </span><p><span style="font-size:medium;">9. When loading the view, specify it to look for the view file in the package. Modify the code in the controller as follows: </span></p><p></p><pre class="brush:php;toolbar:false">public function getRegister()    
{ 
    return View::make('package::test');    
}
Refresh the page again, and the page we are looking forward to appears,

public migration

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

Create database migration

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

执行数据库迁移

php artisan migrate --bench="vendor/package"

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!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete