LaravelComposerPackage development practice toastr-for-laravel5

Source: Internet
Author: User
LaravelComposerPackage development practice toastr-for-laravel5 this article original link from my blog, address: Laravel Composer Package development practice toastr-for-laravel5

Package Development is included in Laravel documentation, which is abstract for beginners. to develop a Package, you need to understand Service Providers, service Providers and Facade are abstract enough for developers who are new to Laravel, so let's write a simple example tutorial for Laravel package development.

Toastr. js is a very convenient notification effect. laravel 5.2 has just been released recently, so let's develop a toastr for laravel 5 package, we mainly use toastr in combination with laravel's flash session to implement one-time message reminders on the page. In fact, this page Operation reminder is very common in our daily development.

The general laravel package development process is like this. after the development, pack and push the package to gitlab, and then submit the packagist. next we will implement this process step by step.

1. create the following packages/yuansir/toastr/src directory in the newly created laravel project. the packages directory is the same as the app directory. The code of our development kit is stored in this src directory, and yuansir and toastr are completely customized.

2. modify the project's composer. json and set the PSR-4 namespace:

"autoload": {        "classmap": [            "database"        ],        "psr-4": {            "App\\": "app/",            "Yuansir\\Toastr\\": "packages/yuansir/toastr/src/"        }    },

Do not forget to execute autoload

$ cd pacages/yuansir/toastr/src

3. initialize a composer. json file for our package. anyone familiar with composer should know what this is.

$ cd pacages/yuansir/toastr/src
Follow the prompts to fill in the relevant information. you do not need to fill in some information. you can add it in composer. json. the generated example is as follows:
{    "name": "ryan/toastr-for-laravel",    "description": "toastr.js for laravel5",    "authors": [        {            "name": "Ryan",            "email": "yuansir@live.cn"        }    ],    "require": {}}

4. start development and create a Service Provider.

php artisan make:provider ToastrServiceProvider

Move the generated app/Providers/ToastrServiceProvider. php file to our packages/yuansir/toastr/src directory, and register ToastrServiceProvider to the providers of config/app. php.

'providers' => [        /*         * Laravel Framework Service Providers...         */         ......        /*         * Application Service Providers...         */         ......        Yuansir\Toastr\ToastrServiceProvider::class,    ],

5. create packages/yuansir/toastr/src/config/toastr. php to save the options of toastr. js. There are many options configurations. for details, refer to its demo.

  []];

We will talk about how to call this configuration in our package later.

6. create a Toastr class to implement the toastr info, success, error, and warning implementations. the code is still very simple. packages/yuansir/toastr/src/Toastr. php:

 session = $session;        $this->config = $config;    }    public function render()    {        $notifications = $this->session->get('toastr:notifications');        if(!$notifications) {            return '';        }        foreach ($notifications as $notification) {            $config = $this->config->get('toastr.options');            $javascript = '';            $options = [];            if($config) {                $options = array_merge($config, $notification['options']);            }            if($options) {                $javascript = 'toastr.options = ' . json_encode($options) . ';';            }            $message = str_replace("'", "\\'", $notification['message']);            $title = $notification['title'] ? str_replace("'", "\\'", $notification['title']) : null;            $javascript .= " toastr.{$notification['type']}('$message','$title');";        }        return view('Toastr::toastr', compact('javascript'));    }    /**     * Add notification     * @param $type     * @param $message     * @param null $title     * @param array $options     * @return bool     */    public function add($type, $message, $title = null, $options = [])    {        $types = ['info', 'warning', 'success', 'error'];        if(!in_array($type, $types)) {            return false;        }        $this->notifications[] = [            'type' => $type,            'title' => $title,            'message' => $message,            'options' => $options        ];        $this->session->flash('toastr:notifications', $this->notifications);    }    /**     * Add info notification     * @param $message     * @param null $title     * @param array $options     */    public function info($message, $title = null, $options = [])    {        $this->add('info', $message, $title, $options);    }    /**     * Add warning notification     * @param $message     * @param null $title     * @param array $options     */    public function warning($message, $title = null, $options = [])    {        $this->add('warning', $message, $title, $options);    }    /**     * Add success notification     * @param $message     * @param null $title     * @param array $options     */    public function success($message, $title = null, $options = [])    {        $this->add('success', $message, $title, $options);    }    /**     * Add error notification     * @param $message     * @param null $title     * @param array $options     */    public function error($message, $title = null, $options = [])    {        $this->add('error', $message, $title, $options);    }    /**     * Clear notifications     */    public function clear()    {        $this->notifications = [];    }}




7. view ('toastr: toastr', compact ('javascript ');, you need a view file :: what is toastr? we will discuss how to create packages/yuansir/toastr/src/views/toastr. blade. php view file:

 

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.