Error solving tasks before Spug publishing: Exception: 'utf-8' codec can't decode bytes in position 116-117: invalid continuation byte Author: Chuwen Time: 2021-09-12 Classification: Linux comment ##Environment Briefly describe my environment. Docker is installed on macOS, and Spug is installed in Docker. Then add a host in Spug, connect the local machine with ssh, and simulate the relevant environment to build and publish - Docker for macOS: `Docker version 20.10.8, build 3967b7d` -Spug API Version: ` v3.0.1-beta.9` -Spug Web Version: ` v3.0.1-beta.9` ##Task error before publishing Exception: 'utf-8' codec can't decode bytes in position 116-117: invalid continuation byte ##Investigate the reason Until I use the Web Console to connect to the local ssh and execute the'll 'command, I found that the folder name is'` At first glance, I found the problem. It was Chinese garbled code. I thought that the above error might be the reason. ##Solution Suppose you use 'oh my zsh' and execute the following command: ```shell vim ~/.zshrc ``` Then add the following two sentences at the end of the file: ``` export LC_ALL=zh_CN.UTF-8 export LANG=zh_CN.UTF-8 ``` Then save and exit, first press' ESC ', then enter': wq 'in English input status, and then press' Enter' Then make the configuration file effective: ```shell source ~/.zshrc ```
Docker obtains the IP address of the host Author: Chuwen Time: 2021-09-12 Classification: Docker comment I didn't find anything related online This domain name gets the IP of the host: ` host. docker. internal` So you should know how to use it ``` [ root@3905274ca2a3 /]# ping host.docker.internal PING host.docker.internal (192.168.65.2) 56(84) bytes of data. 64 bytes from 192.168.65.2 (192.168.65.2): icmp_seq=1 ttl=37 time=0.173 ms 64 bytes from 192.168.65.2 (192.168.65.2): icmp_seq=2 ttl=37 time=0.524 ms 64 bytes from 192.168.65.2 (192.168.65.2): icmp_seq=3 ttl=37 time=0.248 ms 64 bytes from 192.168.65.2 (192.168.65.2): icmp_seq=4 ttl=37 time=0.515 ms ^C --- host.docker.internal ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3039ms rtt min/avg/max/mdev = 0.173/0.365/0.524/0.156 ms ```
PHPStorm cancels automatic addition to git when creating a new file (execute git add {filename}) Author: Chuwen Time: 2021-09-07 Classification: Software comment ###PHPStorm cancels automatic addition to git when creating a new file (execute git add {filename}) Just follow the steps below ! [iShot2021-09-07 15.26.49.jpg]( https://cdn.nowtime.cc/2021/09/07/2429317775.jpg )
Write PHP code more elegantly - keep updating Author: Chuwen Time: 2021-09-05 Classification: PHP Tips , PHP comment ###Basic PHP 8.0+ ####Named parameters ```php // PHP 7 htmlspecialchars($string, ENT_COMPAT | ENT_HTML401, 'UTF-8', false); // PHP 8 htmlspecialchars($string, double_encode: false); ``` ###Notes >Now you can use PHP native syntax to use structured metadata instead of PHP Doc declaration. ```php // PHP 7 class PostsController { /** * @Route("/api/posts/{id}", methods={"GET"}) */ public function get($id) { /* ... */ } } // PHP 8 class PostsController { #[Route("/api/posts/{id}", methods: ["GET"])] public function get($id) { /* ... */ } } ``` ####Constructor attribute promotion ```php // PHP 7 class Point { public float $x; public float $y; public float $z; public function __construct( float $x = 0.0, float $y = 0.0, float $z = 0.0 ) { $this->x = $x; $this->y = $y; $this->z = $z; } } // PHP 8 class Point { public function __construct( public float $x = 0.0, public float $y = 0.0, public float $z = 0.0, ) {} } ``` ####Union Type >Compared with the previous combination of PHPDoc declaration types, it can now be replaced by the native supported union type declaration, which is verified at runtime. ```php // PHP 7 class Number { /** @var int|float */ private $number; /** * @param float|int $number */ public function __construct($number) { $this->number = $number; } } new Number('NaN'); // Ok // PHP 8 class Number { public function __construct( private int|float $number ) {} } new Number('NaN'); // TypeError ``` ####Match expression ```php // PHP 7 switch (8.0) { case '8.0': $result = "Oh no!"; break; case 8.0: $result = "This is what I expected"; break; } echo $result; //> Oh no! // PHP 8 echo match (8.0) { '8.0' => "Oh no!", 8.0 => "This is what I expected", }; //> This is what I expected ``` ####Nullsafe operator ```php // PHP 7 $country = null; if ($session !== null) { $user = $session->user; if ($user !== null) { $address = $user->getAddress(); if ($address !== null) { $country = $address->country; } } } // PHP 8 $country = $session?-> user?->getAddress()?-> country; ``` ---- ###Enumeration 1. Using PHP 8.1, as of September 4, 2021, version 8.1 has not been officially released 2. Use expansion packs to achieve: https://github.com/myclabs/php-enum ```shell composer require myclabs/php-enum ``` ###JSON to Class makes IDEA syntax prompt > Github: https://github.com/cweiske/jsonmapper ```shell composer require netresearch/jsonmapper ``` ###Generics https://github.com/mrsuh/php-generics
Make a Larvel Model insertion or update note, because there are many mistakes Author: Chuwen Time: 2021-09-03 Classification: Laravel , PHP 1 comment ###Create new data or update data for Larravel model >Maybe my method is not elegant enough. You are welcome to criticize me ```php <? php namespace App\Models; Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Content extends Model { use HasFactory; Protected $dateFormat='U ';//U: timestamp //Create new data or update data public static function insertOrUpdate(int $id, int $uid) { //Find data first $content = Content::whereId($id)->first(); //If empty if (is_null($content)) { //Create model instance $content = new Content; $content->uid = $uid; } $content ->text='Test'; //Create new data or update data $content->save(); } } ``` Leave a link to the Chinese document: https://learnku.com/docs/laravel/8.x/eloquent/9406#c7d398