php - 如何在 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();
}
}
解决方案
您必须在控制器名称之后传递函数名称。
就像您的路径:http: //192.168.122.1/project_name/auth_admin/signin它将起作用。
如果这不起作用,请添加index.php
并检查它,因为这取决于您如何设置项目。
推荐阅读
- java - Kinesis Aync 客户端 2.x 给出 Http2ConnectionTerminatingException
- javascript - 根据输入值动态包含刀片模板
- ios - 如何在 UIKit UiSlider 中为自定义 thumbImage 添加阴影?
- c++ - Visual Studio C++ - 模板类 LNK2019 错误
- php - Pico CMS - 标签插件设置和分页
- r - 如何将一个字符串分成多个变量
- reactjs - 来自 json 的 Reactjs setstate
- c# - Sonarqube - 扩展嵌套的三元运算符
- python - Pandas read_csv -- 无法解析字符串“FALSE”
- sql - 如果使用 SQL 找不到所需的值,如何显示 null