Example of custom package development in Laravel 5.1

Source: Internet
Author: User
Tags json

1. Install Laravel

If Laravel has been installed, skip this step. For details about installation and configuration of Laravel, see Laravel 5.1 installation and configuration.

2. Create directories and service providers

Create the packages/jai/contact/src folder in the root directory.

Go to the src directory and create a service provider ContactServiceprovider. php:

<? Php namespace Jai \ Contact;

Use Illuminate \ Support \ ServiceProvider;
Use Illuminate \ Routing \ Router;
Class ContactServiceprovider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
     *
* @ Var bool
*/
Protected $ defer = false;
Public function boot ()
    {
$ This-> loadViewsFrom (realpath (_ DIR _. '/../views'), 'Contact ');
$ This-> setupRoutes ($ this-> app-> router );
// This for conig
$ This-> publishes ([
_ DIR _. '/config/contact. Php' => config_path ('contact. Php '),
]);
    }

/**
* Define the routes for the application.
     *
* @ Param \ Illuminate \ Routing \ Router $ router
* @ Return void
*/
Public function setupRoutes (Router $ router)
    {
$ Router-> group (['namespace' => 'Jai \ Contact \ Http \ controllers'], function ($ router)
        {
Require _ DIR _. '/Http/routes. Php ';
});
    }

Public function register ()
    {
$ This-> registerContact ();
Config ([
'Config/contact. Php ',
]);
    }
Private function registerContact ()
    {
$ This-> app-> bind ('Contact ', function ($ app ){
Return new Contact ($ app );
});
    }
}

Create a route

Create an Http directory under the src directory and create a routes. php:

<? Php

Route: get ('Contact ', 'contactcontroller @ index ');

Create a controller

Create the Controllers directory in the Http directory and create the controller ContactController. php:

<? Php namespace Jai \ Contact \ Http \ Controllers;

Use App \ Http \ Controllers \ Controller;
Use Illuminate \ Support \ Facades \ Config;

Class ContactController extends Controller
{
/**
* Show the application welcome screen to the user.
     *
* @ Return Response
*/
Public function index ()
    {
Dd (Config: get ("contact. message "));
Return view ('contact: contact ');
    }
}

Create a configuration file

Create a config directory in the src directory and create the configuration file contact. php:

<? Php
Return [
"Message" => "Welcome to your new package"
];

Create a view file

Create the views folder under the packages/jai/contact directory and create the view file template. blade. php:

<Html>
<Head>
<Meta charset = "UTF-8">
<Title> title </title>
</Head>
<Body>
@ Yield ('content ')
</Body>
</Html>

And view file contact. blade. php:

@ Extends ('contact: template ')
@ Section ('content ')
Laravel Academy
@ Stop

The generated directory structure is as follows:

3. Add the package path to composer. json.

Modify composer. json in the root directory and add "Jai \ Contact \": "packages/jai/contact/src/" to the psr-4:

"Psr-4 ":{
"App \": "app /",
"Jai \ Contact \": "packages/jai/contact/src /"
}

4. Register a service provider

Modify config/app. php in the root directory of the Laravel application and append the service provider to the providers array:

Jai \ Contact \ ContactServiceProvider: class

5. Load packages and release resources

After completing the preceding operations, you must execute two commands in the command line. First, run the command in the application root directory.

Composer dump-autoload

To update the autoloader of Composer, and then run

Php artisan vendor: publish

Publish the configuration file of the custom package to the config Directory of the application root directory for access.

6. Test in a browser

After completing this operation, you can access the http://laravel.app: 8000/contact in the browser, the page output is as follows:

"Welcome to your new package"

The code for modifying ContactController. php is as follows:

// Dd (Config: get ("contact. message "));
Return view ('contact: contact ');

The page output is:

Laravel Academy

This indicates that the custom package has been developed and passed the test.

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: info-contact@alibabacloud.com 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.