Create Packages in Laravel step by step

Source: Internet
Author: User

Create Packages in Laravel step by step

First, let's take a look at Laravel official documentation, this is the latest 4.2 Documentation, if you want to see Chinese words click here, basically the same, this github library setup-laravel4-package, is also a step-by-step introduction to how to create a package, and related resources.

Here we will create an account package as an example:

1. If you use the php artisan workbench command for the first time, you need to configure the name and email in app/config/workbench. php and use the information when generating the package.

2. Use the following command in the project root directory to generate the initial package directory structure. Among them, -- resources is used to generate related resource directories such as view, config, and migrations.

php artisan workbench fstos/account --resources

The root directory of the package is in the workbench/fstos/account directory. Its structure is roughly as follows:

/src    /Fstos        /Account            AccountServiceProvider.php    /config    /lang    /migrations    /views/tests/public

3. Add the 'fstos \ Account \ accountserviceprovider' of the package to the "providers" array in app/config/app. php, so that the package will be loaded into the application.

4. Add an independent route file for this package. You only need to add the following code to the boot function of AccountServiceProvider and create the routes. php file in the root directory of the package.

public function boot(){    $this->package('fstos/account');    include __DIR__.'/../../routes.php';}

The routing file is as follows:

Route::get('account/register',  function(){    return "account register router";});

5. OK. Now you can test it in your browser. "account register router" is displayed. Next we will try to use controller


6. Modify routes as follows:

Route::get('account/register',         array('as' => 'account.register.get',             'uses' => 'Fstos\Account\RegistrationController@getRegister')        );

7. Create the RegistrationController. php file in controllers. The Code is as follows:

 Layout) {$ this-> layout = View: make ($ this-> layout );}}}

Try to refresh the page. Aha, is it an error? "Fstos \ Account \ RegistrationController" cannot be found. Laravel does not automatically search for any directories by default. You need to specify the directory where the files are stored by the developer, however, it also provides a suggested path. For example, if the src/controllers directory is generated when artisan workbench is used, OK. Just add the automatic loading path.

8. composer in the package root directory. add "src/controllers" to the classmap field under autoload in json, and run php artisan dump-autoload and okok in the project root directory. Refresh the page, haha.

9. Next we will use the views file! Well, according to our understanding, we only need to modify the code in the controller, as shown below:

    public function getRegister()    {        return View::make('account.register');    //  return "yes,use controller with route in packages!";    }

Apparently, he accessed the app/views/account/register. blade. php view template. We created this file and wrote the following content:

    we are shown in views!

Refresh the page! Hahaha, fortunately, this rule ~, However, if we create a package, we don't want to place the view in the package in the app/views directory. It's a drop. We must integrate it into the package. Remember that there is also a view under src, we put the file in the src/views/account/register package. blade. in php, the content is as follows:

    we are shown in views under workbench!

Refresh the page. I will clean the page. Is Laravel preferentially loading the template file under app/views? Delete app/views/account/and refresh the page again, again disappointed, the not found page appeared again. In Laravel, view files in the package are not automatically indexed, so we still need to do a little work!

10. When loading a view, specify it to search for the view file in the package. modify the code in the controller as follows:

    public function getRegister()    {        return View::make('account::account.register');    //  return "yes,use controller with route in packages!";    }

Refresh the page again. The expected page appears. We can see that the account command space is used in the controller (For details, refer to the laravel official documentation ), so where is this space specified?

11. Let's take a look at AccountServiceProvider. $ this-> package ('fstos/account') in the boot function in the PHP file. this line of code actually specifies the Command Space of this package and the directory it is bound, this function also has the second (space name) and third (directory) parameters. However, both parameters have default values. The default value of namespace is account, the default directory is the src directory under the root directory, which is equivalent to the following statement:


OK, you can try ~

In the episode, I wrote it twice from the first record. It took a long time to log on to the system during the first record, but the record was lost. I tried it all, and I was so embarrassed, think about how important data backup is ~

This is my first release location:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.