hyperf FAQ summary (longer term)

Posted by Zephyr_Pure on Sat, 25 Dec 2021 12:37:24 +0100

Environment php8 0 hyperf2. 2 swoole4. five

1. use Hyperf\Di\Annotation\Inject must be introduced to instantiate use class with annotation;

2. To achieve the effect of laravel thinkphp using cache, you need to define your own class

<?php
namespace App\Library;

use Hyperf\Utils\ApplicationContext;
use Psr\SimpleCache\CacheInterface;

class Cache
{
	public static function instance()
	{
		return ApplicationContext::getContainer()->get(CacheInterface::class);
	}

	public static function __callStatic($name, $arguments)
	{
		return self::getInstance()->$name(...$arguments);
	}
}

//

Cache::instance()->get() // or set

3. Use the tp view template engine php8 In case of 0, an error will be reported ob_ implicit_ The flush function is defined in SY records // Template. Line 263 under PHP ob_implicit_flush(0); 0 in should be changed to false

Because declare (strict_types = 1); For the sake of , ob_implicit_flush must pass in a Boolean value of 0, and an error will be reported

4. Do not separate the front and back end of the program. The function of background login to automatically maintain login requires the stored value of cookie record, but hyperf is not ideal for cookie operation, but the sessionid of hyperf will not change without cleaning the cache. Therefore, the automatic login function can be realized by using cache redis to record the stored value of sessionid

5.jwt login verification cannot use the components of phper666, because it has been disconnected for a year and only supports php7, so I chose this article

        https://blog.csdn.net/sluckyboy/article/details/116792138

It is simple and easy to use less than one page of code to solve the demand. Auth permission is written by myself. I moved the auth permission component of tp

Because most of the hyperf are the auth components of laravel, I'm used to tp. It's better to move tp

6. Compared with tp, the field of tp in database operation is replaced with select and cannot be implemented as' id,bane '. select('1','2') is required

The select method is replaced by the get() method. The model query cannot be directly statically and coherently operated. You must admin:: query() - > and then coherently operated

Find is replaced by the first method, but it can be used by find (primary key), so there is no need to write the where condition. The first method must be used for writing the where submission

Other changes can be made by looking at the document. hyperf also uses the database component of laravel. You can also directly look at the laravel document

The getpk and other methods of tp need to be encapsulated by themselves

7. Take ip

        $res = $this->request->getServerParams();
		if(isset($res['http_client_ip'])){
			return $res['http_client_ip'];
		}elseif(isset($res['http_x_real_ip'])){
			return $res['http_x_real_ip'];
		}elseif(isset($res['http_x_forwarded_for'])){
			$arr = explode(',',$res['http_x_forwarded_for']);
			return $arr[0];
		}else{
			return $res['remote_addr'];
		}

8. According to the auth permission written by itself, the component needs a thing like tp to obtain the model controller method. Hyperfs such as requset () - > controller () method do not need to be encapsulated by themselves

        $info = $this->request->getAttribute(Dispatched::class)->handler->callback;
		$info = explode("\\", $info);
		$module = $info[2];
		$info = explode("@", $info[3]);
		$controller = str_replace('Controller', '', $info[0]);
		$action = $info[1];
		return [
			'module'=>$module,
			'controller'=>$controller,
			'action'=>$action,
		];

The above method name can also be used for the view, because each render method of the view needs to pass in a file name, which is very troublesome. Just get it automatically and name it in folders according to the module control method. The following is my own encapsulated method

<?php

namespace App\Library;

use Hyperf\Di\Annotation\Inject;
use Hyperf\View\RenderInterface;

class View
{
    /**
     * @Inject
     * @var Core
     */
    protected $core;

    /**
     * @Inject
     * @var RenderInterface
     */
    protected $view;

    public function render($name='',$config=[])
    {
        $name = $name ?: $this->core->getInfo(); // getInfo is Admin/Index/login
        return $this->view->render($name, $config);
    }
}
//
new View();
$view->render();// There is no need to pass any value. If it is the login method of the Index controller of the Admin module, it will automatically locate to example: storage / view / Admin / Index / login html

Update slowly.......

Topics: PHP swoole