美文网首页Laravel Steps
Laravel Provider Register的用法

Laravel Provider Register的用法

作者: AnnaJIAN | 来源:发表于2018-11-01 22:19 被阅读0次

写一个登录成功之后自动打印欢迎消息的小例子

两种绑定的方法

php artisan make:provider NoticationServiceProvider

绑定一个class,调用的时候直接实例化这个class

namespace App\Providers;
use Illuminate\Support\ServiceProvider;

class NotificationServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind('notification.forUser', 'App\Library\Services\NotificationForUser');
    }
}
NotificationServiceInterface.php
namespace App\Library\Services\Contracts;
  
Interface NotificationServiceInterface
{
    public function printNotice();
}
Notification.php
namespace App\Library\Services;

use App\Library\Services\Contracts\NotificationServiceInterface;

class NotificationForUser implements NotificationServiceInterface
{
    public function printNotice()
    {
      return 'Welcome ' . auth()->user()->name . ' !';
    }
}

调用,登录之后printNotice

public function store()
{
    if (!auth()->attempt(request(['name', 'password']))) {
        return back()->withErrors([
            'message' => "Please check your credientials and try again."
        ]);
    }

    // Get notice from session('notice') in template.
    return redirect()->home()->with('notice' , app('notification.forUser')->printNotice());
}

Redirect 之后,参数只能通过session来传递了。
所以blade.php里面用session

@if (session('notice'))
    <div class="alert alert-success">
        {{ session('notice') }}
    </div>
@endif 

绑定一个接口,调用的时候直接实例化这个接口

!!需要在调用的类中注入接口才会生效

namespace App\Providers;
use Illuminate\Support\ServiceProvider;

class NotificationServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind('App\Library\Services\Contracts\NotificationServiceInterface',
            function ($app) {
                return new \App\Library\Services\NotificationForUser();
        });
    }
}
use App\Library\Services\Contracts\NotificationServiceInterface;
...
public function store(NotificationServiceInterface $NotificationForUserInstance)
{
    if (!auth()->attempt(request(['name', 'password']))) {
        return back()->withErrors([
            'message' => "Please check your credientials and try again."
        ]);
    }

    // Get notice from session('notice') in template.
    return redirect()->home()->with('notice' , $NotificationForUserInstance->printNotice());
}

最后结果差不多这样,登录之后自动触发


Print Notice

相关文章

  • Laravel Provider Register的用法

    写一个登录成功之后自动打印欢迎消息的小例子 两种绑定的方法 绑定一个class,调用的时候直接实例化这个class...

  • 大白话剖析Dubbo架构图

    1、启动provider zookeeper.ZookeeperRegistry: [DUBBO]Register...

  • Laravel 开发笔记

    Laravel 笔记 前言 记录 Laravel 开发中的问题,及笔记。 用法 Validator 类的用法 用法...

  • 3.dubbo开撸

    1.大体涉及到的 生产者:register、protocol、service、meithod、provider 消...

  • laravel 如何自定义provider

    provider是laravel程序的核心所在,包括你自己的应用程序,以及所有的 Laravel 核心服务,都是通...

  • flutter Provider的用法

    Provider 是状态管理的官方推荐的状态管理的库.flutter官方文档针对状态管理还提供了使用Provide...

  • Dubbo的⼯作原理?

    1. 服务启动的时候,provider和consumer根据配置信息,连接到注册中⼼register,分别向注册中...

  • 基于umeng官方php sdk v1.4,支持Laravel5

    安装 Laravel 5.* 配置 打开config目录下的app.php文件,找到provider,添加如下代码...

  • 使用 Laravel Excel 库

    使用 Laravel Excel 库 这里简单介绍一下 Laravel-Excel 3.1 用法 官网资料 导出 ...

  • React -Redux

    一个函数两个括号的用法 安装 在最外层App.js里面引用Provider,然后用Provider标签包裹App组...

网友评论

    本文标题:Laravel Provider Register的用法

    本文链接:https://www.haomeiwen.com/subject/agefxqtx.html