常忘 发布的文章


class HandleInertiaRequests extends Middleware
{
    public function handle(Request $request, Closure $next)
    {
        $response =  parent::handle($request, $next);
        if($response->isRedirection()) {
            $redirectTo = $response->headers->get('Location');
            $host = data_get(parse_url(config('app.url')), 'host');
            Log::debug('redirect to: '. $redirectTo);
            $redirectHost = data_get(parse_url($redirectTo), 'host');
            if($host !== $redirectHost) {
                return Inertia::location($redirectTo);
            }
        }
        return $response;
    }
}

OS: Ubuntu 22, Apache2 has beeen installed

enabled rewrite

sudo a2enmod rewrite

add .htaccess to replace index.php

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.*)$ index.php/$1 [L]
RewriteRule ^(.*)$ index.php [E=PATH_INFO:$1,QSA,PT,L]
</IfModule>

# Disable directory browsing
Options -Indexes

function buildOutputSheet(array $data, $title, array $columns)
{
       $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
        $worksheet = $spreadsheet->getActiveSheet();
        $worksheet->setTitle($title);

        // write the cell header(the first row)
        $worksheet->fromArray([array_values($columns)], null, 'A1');
        // writing data
        foreach ($data as $row => $item) {
            foreach (array_keys($columns) as $col => $field) {
                $value = $item[$field];
                $worksheet->setCellValueByColumnAndRow($col + 1, $row + 2, $value);
            }
        }
        $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
        $filename = now()->format('Ymd-') . \Illuminate\Support\Str::random(6) . '.xlsx';
        // got the file store path.
        $file_path = storage_path("app/public") . "/$filename";
        // save to file.
        $writer->save($file_path);
}

trait class 的定义

namespace App\Library\Model;

use Illuminate\Contracts\Validation\Factory;

trait WithValidates
{

//    protected $rules = [];
//    protected $customMessages = [];
//    protected $customAttributes = [];

    /**
     * @param $validator
     *
     * @return array
     */
    public function validateWith($validator)
    {
        if (is_array($validator)) {
            $validator = $this->getValidator($this->attributes, $validator);
        }
        $validator->after(function ($validate) {
            return call_user_func('self::afterValidate', $validate);
        });
        return $validator->validate();
    }

    protected function getValidator($data, $rules = [], $messages = [], $customAttributes = [])
    {
        return $this->getValidationFactory()->make($data, $rules ?: [], $messages ?: [], $customAttributes ?: []);
    }

    /**
     * Get a validation factory instance.
     * @return Factory
     */
    protected function getValidationFactory()
    {
        return app(Factory::class);
    }

    /**
     * @param null $rules
     * @param null $messages
     * @param null $customAttributes
     *
     * @return array
     */
    public function validate($rules = [], $messages = [], $customAttributes = [])
    {
        $validator = $this->getValidator(
            $this->attributes,
            $rules ?: $this->rules,
            $messages ?: $this->customMessages,
            $customAttributes ?: $this->customAttributes
        );
        $validator->after(function ($validate) {
            return call_user_func('self::afterValidate', $validate);
        });
        return $validator->validate();
    }

    /**
     * @param $validator
     */
    protected function afterValidate($validator)
    {

    }

}

简单使用

namespace App;

use Illuminate\Database\Eloquent\Model;

class Tooltip extends Model
{
    use WithValidates;
    protected $rules = [
        'category' => 'string|required|max:64',
        'field' => 'string|required|max:64',
        'index' => 'integer|nullable',
        'description' => 'string|nullable',
    ];

    protected $fillable = [
        'category',
        'field',
        'index',
        'description',
    ];
}

abstract class 的方式来使用

abstract class BasicModel extends Model
{
    use WithValidates;

    protected $perPage = 10;
    protected $rules = [];
    protected $customMessages = [];
    protected $customAttributes = [];
}

model file usage

namespace App;

use App\Library\Model\BasicModel;

class Tooltip extends BasicModel
{
    protected $rules = [
        'category' => 'string|required|max:64',
        'field' => 'string|required|max:64',
        'index' => 'integer|nullable',
        'description' => 'string|nullable',
    ];

    protected $fillable = [
        'category',
        'field',
        'index',
        'description',
    ];
}

git tags

  1. Delete the tag on any remote before you push

    git push origin :refs/tags/<tagname>
  2. Replace the tag to reference the most recent commit

    git tag -fa <tagname>
  3. Push the tag to the remote origin

    git push origin master --tags
  4. create a new tag

    git tag -a <tagname>