首页 > 解决方案 > 如何在 Codeigniter 中制作 Restful API?

问题描述

我在尝试我制作的 API 时遇到问题。当我在 Postman 中测试它时,它看起来是错误的,即使我的用户名和密码是正确的。你能找出我的代码的问题吗?我真的很感谢你的帮助。

Auth_admin.php

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

require APPPATH . '/libraries/REST_Controller.php';

class Auth_admin extends REST_Controller
{
    public function signin_post()
    {
        $this->load->model('model_admin', 'admin');
        $params = array(
                'username' => $this->post('username'),
                'password' => md5($this->post('password'))  
    );
    $result = $this->admin->admin_check($params);
    if ($result){
        if ($result->level == "admin"){

            $response   = array(
                "status"    => true,
                "message"   => "Authentication seccessfully",
                "auth"      => array(
                    "username"  => $result->username
                )
            );
            $this->set_response($response, REST_Controller::HTTP_OK);
        }else{
            $response   = array(
                "status"    => false,
                "message"   => "This features does'nt exist for your Account"
            );
            $this->set_response($response, REST_Controller::HTTP_OK);
        }
    }
}
    }

模型管理.php

<?php

class Model_admin extends CI_Model
{
    var $tablename;

    public function __construct()
    {
        parent::__construct();
        $this->tablename    = "m_admin";
    }

    public function admin_check($data = array())
    {
        $params = array(
            'username' => $data['username'],
            'password' => $data['password'],
        );
        $this->db->where($params);
        $query  = $this->db->get($this->tablename);
        return $query->row();
    }

}

邮递员结果

标签: phprestapicodeigniter

解决方案


您必须在控制器名称之后传递函数名称。

就像您的路径:http: //192.168.122.1/project_name/auth_admin/signin它将起作用。

如果这不起作用,请添加index.php并检查它,因为这取决于您如何设置项目。


推荐阅读