zoukankan      html  css  js  c++  java
  • 服务提供者

    1.新建控制器

    php artisan make:controller HelloController
    <?php
    
    namespace AppHttpControllers;
    
    use IlluminateHttpRequest;
    
    class HelloController extends Controller
    {
        public function index()
        {
            echo '服务调用';
        }
    }

    2.新建服务提供者

    php artisan make:provider HelloServiceProvider
    <?php
    
    namespace AppProviders;
    
    use AppHttpControllersHelloController;
    use IlluminateSupportServiceProvider;
    
    class HelloServiceProvider extends ServiceProvider
    {
        /**
         * Register services.
         *
         * @return void
         */
        public function register()
        {
            //注册服务提供者,单例模式
            $this->app->singleton('hello', function () {
                return new HelloController();
            });
    
            //或者
            // $this->app->bind('hello', HelloController::class);
        }
    
        /**
         * Bootstrap services.
         *
         * @return void
         */
        public function boot()
        {
            //
        }
    }

    3.在configapp.php的providers数组中注册服务提供者

    'providers' => [
           ...  
            AppProvidersHelloServiceProvider::class,
        ],

    4.新建路由

    Route::get("helloservice",function(){
        app("hello")->index();
    });

    5.页面调用

    php artisan make:provider HellServiceProvider

  • 相关阅读:
    4Sum
    3Sum Closest
    3Sum
    Longest Common Prefix
    Roman to Integer
    thinkphp3.2自定义配置文件
    centos7下git的使用和配置
    git 报错
    Git服务器安装详解及安装遇到问题解决方案
    centos GIT安装
  • 原文地址:https://www.cnblogs.com/clubs/p/15187406.html
Copyright © 2011-2022 走看看