PHP code example of lianglong / hyperf-tenancy

1. Go to this page and download the library: Download lianglong/hyperf-tenancy library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lianglong / hyperf-tenancy example snippets


    
    
    declare(strict_types=1);
    /**
     * This file is part of Hyperf.
     *
     * @link     https://www.hyperf.io
     * @document https://hyperf.wiki
     * @contact  [email protected]
     * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
     */
    namespace App\Listener;
    
    use Hyperf\Event\Annotation\Listener;
    use Hyperf\Event\Contract\ListenerInterface;
    use Hyperf\Framework\Event\AfterWorkerStart;
    use Hyperf\HttpMessage\Exception\ForbiddenHttpException;
    use Stancl\Tenancy\Middleware;
    use Stancl\Tenancy\Resolvers\PathTenantResolver;
    use Stancl\Tenancy\Resolvers\RequestDataTenantResolver;
    use Psr\Container\ContainerInterface;
    
    /**
     * @Listener
     */
    class TenancyInitListener implements ListenerInterface
    {
    
        /**
         * @var ContainerInterface
         */
        private $app;
    
        public function __construct(ContainerInterface $container)
        {
            $this->app = $container;
        }
    
        public function listen(): array
        {
            return [
                AfterWorkerStart::class
            ];
        }
    
        public function process(object $event)
        {
    
            /**
             * ByRequestData 租户识别方式相关设置
             */
            //租户匹配来源字段(header)
            Middleware\InitializeTenancyByRequestData::$header = 'x-tenant';
            //禁用通过查询参数匹配租户
            Middleware\InitializeTenancyByRequestData::$queryParameter = null;
            //开启租户信息缓存 #see https://tenancyforlaravel.com/docs/v3/cached-lookup
            RequestDataTenantResolver::$shouldCache = true;
            //设置租户信息缓存有效时间
            RequestDataTenantResolver::$cacheTTL = 86400;
            //租户匹配失败时提示
            Middleware\InitializeTenancyByRequestData::$onFail = function (\Exception $e, \Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Server\RequestHandlerInterface $next){
                throw new ForbiddenHttpException();
            };
            /**
             * ByPath 租户识别方式相关设置
             */
            //租户匹配来源字段 route param
            PathTenantResolver::$tenantParameterName = 'tenant';
            //开启租户信息缓存
            PathTenantResolver::$shouldCache = true;
            //设置租户信息缓存有效时间
            PathTenantResolver::$cacheTTL = 86400;
            //租户匹配失败时提示
            Middleware\InitializeTenancyByPath::$onFail = function (\Exception $e, \Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Server\RequestHandlerInterface $next){
                throw new ForbiddenHttpException();
            };
        }
    }

    # composer hyperf.php tenants:install