php - How to access the update method after using resource controller and resource routing in tp5 (already solved)
習慣沉默
習慣沉默 2017-06-07 09:23:14
0
1
915

How do I access update when using resource routing and resource controller in tp5? I can access edit save index, but update cannot access it

//资源路由
Route::resource('admin','admin/Admin');
//自动生成restful风格控制器
php think make:controller admin/Admin
//Admin控制器所有代码
<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use app\admin\model\AdminModel;
use app\admin\controller\Base;

class Admin extends Base
{   
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $this->pageTitle = '管理员列表';
        $admin = new AdminModel();
        $adminList = $admin->getAdminsPage();
        $page = $adminList->render();
        $this->assign(['adminList' => $adminList, 'page' => $page]);
        return $this->view();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $this->pageTitle = '新增管理员';
        
        return $this->view();
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        
        if ($request->isPost()){
            //上传限制
            $validateArr = ['size'=>2048000, 'ext'=>'jpg,png'];
            //头像上传
            $uploadInfo = $this->upload('avatar', $validateArr);
            if (!$uploadInfo){
                $this->error('上传文件失败' . $uploadInfo);
            }
            //获取数据
            $data = input();
            //对获取的数据进行封装,以下代码纯属个人突然的恶趣味,华而不实
            
            //-----start-------
            $pack_data = [];
            array_walk(
                $data, 
                function($v, $k, $prefix) use (&$pack_data){ 
                    $key = $prefix. $k;
                    $pack_data[$key] = $v;
                },
                'admin_'
            );
            //-----end-------
            
            //推荐常规写法
            /*
            $pack_data = [
                'admin_name' => $data['name'],  
                'admin_passwd' => $data['passwd'],
                'admin_email' => $data['email'],
                'admin_tel' => $data['tel'],
                'admin_qq' => $data['qq'],
                'admin_birth' => $data['birth'],
            ];
            */
            $pack_data['admin_avatar'] = $uploadInfo;
            $admin = new AdminModel();
            $result = $admin->addAdmin($pack_data);
            if (!$result){
                $this->error('新增管理员失败!' . $admin->getError());
            }
            $this->success('新增管理员成功', 'admin/Admin/index');
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        if (!$id){
            $this->error('非法请求!');
        }
        $this->pageTitle = '管理员修改页面';
        $admin = new AdminModel();
        $adminInfo = $admin->getAdminById($id);
        //数据库存生日的类型为datetime,我们在此处做一个转换
        if (isset($adminInfo['admin_birth'])){
            $adminInfo['admin_birth'] = date('Y-m-d', strtotime($adminInfo['admin_birth']));
        }
        $this->assign(compact("adminInfo"));
        return $this->view();
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        if ($id){
            echo $id;
        }
        if ($request->put($id)){
            echo $id;
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        echo 'can\'t see ';
    }
}
//这里设置了虚拟域名
//编辑页面访问路由edit
http://www.siwakongtest.com/index.php/admin/11/edit
//列表路由访问index
http://www.siwakongtest.com/index.php/admin/
//创建资源路由访问create
http://www.siwakongtest.com/index.php/admin/create
//解决方案
<input type="hidden" name="_method" value="PUT">
習慣沉默
習慣沉默

reply all(1)
为情所困

Why can’t you access it? ? What error is reported in debug mode? ? ? I don’t see any problem with the code, unless there is something wrong with your routing settings

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template