ThinKPHP6.0 hands on

Install ThinkPHP6.0

cd to the project directory, and then do the following

Prepare to install the framework: composer create project topthink / think thinkphp6.0

cd switch to the thinkPHP6.0 directory of PHP project

Modify configuration file:. Env configuration file / / copy example.env Change to your own

Open debug mode: app in. env_ DEBUG = true

Display error message: config/app.php Show in_ error_ msg = true

Deploy test run: php think run

You can specify the port: php think run -p 80

Direct access: http://localhost:8000 //: followed by port number

Domain name configuration / URL Rewrite

Build according to your own environment, and remember to add

Apache

<IfModule mod_rewrite.c>
  Options +FollowSymlinks -Multiviews
  RewriteEngine On

  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
</IfModule>

Nginx

location / { // ... . omit some codes
   if (!-e $request_filename) {
   		rewrite  ^(.*)$  /index.php?s=/$1  last;
    }
}

Controller / model / routing

Resource controller: php think make:controller Book

Model: php think make:model Book

Resource route: route:: resource ('Book ','Book');

controller

<?php
declare(strict_types=1);

namespace app\controller;

use app\BaseController;
use app\model\Book as ModelBook;
use think\Request;

class Book extends BaseController
{
    /**
     * Show resource list
     *
     * @return \think\Response
     */
    public function index()
    {
        $res = ModelBook::getPage();
        return json(['code' => 0, 'msg' => 'ok', 'data' => $res]);
    }

    /**
     * The create resource form page is displayed
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * Save new resource
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        // Data reception
        $data['title'] = $request->param('title', 'unknown');
        $data['c_id'] = $request->param('c_id', '1');
        $data['u_id'] = $request->param('u_id', '1');
        $data['desc'] = $request->param('desc', 'unknown');
        $data['img'] = $request->param('img', 'unknown');
        // data validation
        // Perform add
        $res = ModelBook::addOne($data);
        // judge
        if (!$res) {
            return json(['code' => 1, 'msg' => 'Additive failure', 'data' => $res]);
        }
        return json(['code' => 0, 'msg' => 'ok', 'data' => $res]);
    }

    /**
     * Show specified resources
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        $where['id'] = $id;
        $res = ModelBook::getOne($where);
        // judge
        if (!$res) {
            return json(['code' => 1, 'msg' => 'Get failed', 'data' => $res]);
        }
        return json(['code' => 0, 'msg' => 'Success', 'data' => $res]);
    }

    /**
     * The edit resource form page is displayed
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Save updated resources
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        // modify condition
        $where['id'] = $id;
        // receive data 
        $data['title'] = $request->param('title', 'modify');
        $data['c_id'] = $request->param('c_id', '2');
        $data['u_id'] = $request->param('u_id', '2');
        $data['desc'] = $request->param('desc', 'modify');
        $data['img'] = $request->param('img', 'modify');
        // implement
        $res = ModelBook::upd($where, $data);
        // judge
        if (!$res) {
            return json(['code' => 1, 'msg' => 'Modification failed', 'data' => $res]);
        }
        return json(['code' => 0, 'msg' => 'Modification succeeded', 'data' => $res]);
    }

    /**
     * Delete the specified resource
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $where['id'] = $id;
        $res = ModelBook::del($where);
        // judge
        if (!$res) {
            return json(['code' => 1, 'msg' => 'Delete failed', 'data' => $res]);
        }
        return json(['code' => 0, 'msg' => 'Delete succeeded', 'data' => $res]);
    }
}

Model

<?php

namespace app\model;

use think\Model;

class Book extends Model
{
    protected $table = 'book';
    protected $pk = 'id';
    protected $schema = [
        'id' => 'int',
        'c_id' => 'int',
        'u_id' => 'int',
        'title' => 'string',
        'desc' => 'string',
        'img' => 'string',
        'status' => 'int',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
    ];

    /**
     * Get a piece of data
     * @param array $where condition
     */

    public static function getOne($where)
    {
        return self::where($where)->find();
    }

    /**
     * Paging data
     */

    public static function getPage()
    {
        return self::alias('b')->join('book_cate c', 'c.id = b.c_id')->field('b.*, c.name as cateName')->paginate(5);
    }

    /**
     * Add a
     * @param array $data data
     */

    public static function addOne($data)
    {
        return self::create($data);
    }

    /**
     * delete
     * @param array $where condition
     */

    public static function del($where)
    {
        return self::where($where)->delete();
    }

    /**
     * modify
     * @param array $where condition
     * @param array $data data
     */

    public static function upd($where, $data)
    {
        return self::where($where)->save($data);
    }
}

Tags: PHP JSON Apache Nginx

Posted on Fri, 22 May 2020 10:08:32 -0400 by benny_g