zoukankan      html  css  js  c++  java
  • ThinKPHP6.0 上手操作

    安装 ThinkPHP6.0

    cd 切换到项目目录,然后执行如下

    准备安装框架:composer create-project topthink/think thinkPHP6.0

    cd 切换到PHP项目 thinkPHP6.0 目录中

    修改配置文件:.env 配置文件 // 复制 .example.env 改成自己的

    打开调试模式:.env 中的 APP_DEBUG = true

    显示错误消息:config/app.php 中的 show_error_msg = true

    部署测试运行:php think run

    可以指定端口:php think run -p 80

    可以直接访问:http://localhost:8000 // :后面是端口号

    域名配置/URL重写

    根据自己的环境自行搭建,记得在 hosts 中增加

    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 / { // …..省略部分代码
       if (!-e $request_filename) {
       		rewrite  ^(.*)$  /index.php?s=/$1  last;
        }
    }
    

    控制器/模型/路由

    资源控制器:php think make:controller Book

    模型:php think make:model Book

    资源路由:Route::resource('book', 'Book');

    控制器

    <?php
    declare(strict_types=1);
    
    namespace appcontroller;
    
    use appBaseController;
    use appmodelBook as ModelBook;
    use thinkRequest;
    
    class Book extends BaseController
    {
        /**
         * 显示资源列表
         *
         * @return 	hinkResponse
         */
        public function index()
        {
            $res = ModelBook::getPage();
            return json(['code' => 0, 'msg' => 'ok', 'data' => $res]);
        }
    
        /**
         * 显示创建资源表单页.
         *
         * @return 	hinkResponse
         */
        public function create()
        {
            //
        }
    
        /**
         * 保存新建的资源
         *
         * @param  	hinkRequest  $request
         * @return 	hinkResponse
         */
        public function save(Request $request)
        {
            // 数据接收
            $data['title'] = $request->param('title', '未知');
            $data['c_id'] = $request->param('c_id', '1');
            $data['u_id'] = $request->param('u_id', '1');
            $data['desc'] = $request->param('desc', '未知');
            $data['img'] = $request->param('img', '未知');
            // 数据验证
            // 执行添加
            $res = ModelBook::addOne($data);
            // 判断
            if (!$res) {
                return json(['code' => 1, 'msg' => '添加剂失败', 'data' => $res]);
            }
            return json(['code' => 0, 'msg' => 'ok', 'data' => $res]);
        }
    
        /**
         * 显示指定的资源
         *
         * @param  int  $id
         * @return 	hinkResponse
         */
        public function read($id)
        {
            $where['id'] = $id;
            $res = ModelBook::getOne($where);
            // 判断
            if (!$res) {
                return json(['code' => 1, 'msg' => '获取失败', 'data' => $res]);
            }
            return json(['code' => 0, 'msg' => '获取成功', 'data' => $res]);
        }
    
        /**
         * 显示编辑资源表单页.
         *
         * @param  int  $id
         * @return 	hinkResponse
         */
        public function edit($id)
        {
            //
        }
    
        /**
         * 保存更新的资源
         *
         * @param  	hinkRequest  $request
         * @param  int  $id
         * @return 	hinkResponse
         */
        public function update(Request $request, $id)
        {
            // 修改条件
            $where['id'] = $id;
            // 接收数据
            $data['title'] = $request->param('title', '修改');
            $data['c_id'] = $request->param('c_id', '2');
            $data['u_id'] = $request->param('u_id', '2');
            $data['desc'] = $request->param('desc', '修改');
            $data['img'] = $request->param('img', '修改');
            // 执行
            $res = ModelBook::upd($where, $data);
            // 判断
            if (!$res) {
                return json(['code' => 1, 'msg' => '修改失败', 'data' => $res]);
            }
            return json(['code' => 0, 'msg' => '修改成功', 'data' => $res]);
        }
    
        /**
         * 删除指定资源
         *
         * @param  int  $id
         * @return 	hinkResponse
         */
        public function delete($id)
        {
            $where['id'] = $id;
            $res = ModelBook::del($where);
            // 判断
            if (!$res) {
                return json(['code' => 1, 'msg' => '删除失败', 'data' => $res]);
            }
            return json(['code' => 0, 'msg' => '删除成功', 'data' => $res]);
        }
    }
    
    

    模型

    <?php
    
    namespace appmodel;
    
    use thinkModel;
    
    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',
        ];
    
        /**
         * 获取一条数据
         * @param array $where 条件
         */
    
        public static function getOne($where)
        {
            return self::where($where)->find();
        }
    
        /**
         * 分页获取数据
         */
    
        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);
        }
    
        /**
         * 添加一条
         * @param array $data 数据
         */
    
        public static function addOne($data)
        {
            return self::create($data);
        }
    
        /**
         * 删除
         * @param array $where 条件
         */
    
        public static function del($where)
        {
            return self::where($where)->delete();
        }
    
        /**
         * 修改
         * @param array $where 条件
         * @param array $data 数据
         */
    
        public static function upd($where, $data)
        {
            return self::where($where)->save($data);
        }
    }
    
  • 相关阅读:
    八、多线程爬虫之糗事百科案例
    七、数据提取之JSON与JsonPATH
    Day_03
    六、CSS 选择器:BeautifulSoup4
    Day_01
    Day_02
    图解递归函数
    第十章 提权
    提权篇
    Webshell篇
  • 原文地址:https://www.cnblogs.com/laowenBlog/p/12937405.html
Copyright © 2011-2022 走看看