记住这是 codeigniter 4号。
我在这里需要帮助。我正在学习如何在codeigniter 4中实现RestFul API。下面是我的详细代码。
路线:
$routes->resource('ApiManageMaintenance', ['controller' =>'App\Controllers\ApiData\ApiManageMaintenance']); // get, put, create, delete
ApiManageMaintenance.php文件夹:
<?php
namespace App\Controllers\ApiData;
use App\Controllers\BaseController;
use CodeIgniter\RESTful\ResourceController;
class ApiManageMaintenance extends ResourceController
{
function __construct()
{
$model = new Dennis_setting_model();
}
// equal to get
public function index()
{
$Medoo = new \App\Models\Dennis_medoo_model();
$result = $Medoo->SelectAllMaintenance();
$response = [
'status' => 200,
'error' => null,
'messages' => 'Pull Data Successfull',
'data' => $result
];
return json_encode($response);
}
// equal to post
public function create() {
$version = $this->request->getVar('version');
$reason = $this->request->getVar('reason');
if ($version == "" || $reason == "") {
$response = [
'status' => 102,
'error' => 'Data Error',
'messages' => 'Data Not Valid',
'data' => null
];
return json_encode($response);
}
$array = array ('version' => $version,
'reason' => $reason
);
$Medoo = new \App\Models\Dennis_medoo_model();
$Medoo->InsertNewMaintenance($array);
$response = [
'status' => 200,
'error' => null,
'messages' => 'Create New Maintenance Successfull',
'data' => null
];
return json_encode($response);
}
// equal to get
public function show($id = null) {
$Medoo = new \App\Models\Dennis_medoo_model();
$result = $Medoo->SelectAllMaintenance();
$response = [
'status' => 200,
'error' => null,
'messages' => 'Pull Data Successfull',
'data' => $result
];
return json_encode($response);
}
// equal to put
public function update($id = null) {
$data = $this->request->getRawInput();
$data['id'] = $id;
$response = [
'status' => 200,
'error' => null,
'messages' => 'Update Data Successfull',
'data' => null
];
return json_encode($response);
}
// equal to delete
public function delete($id = null) {
$Medoo = new \App\Models\Dennis_medoo_model();
$Medoo->DeleteMaintenance($id);
$response = [
'status' => 200,
'error' => null,
'messages' => 'Delete Data Successfull',
'data' => null
];
return json_encode($response);
}
}
配置过滤器.php
<?php namespace Config;
use CodeIgniter\Config\BaseConfig;
class Filters extends BaseConfig
{
// Makes reading things below nicer,
// and simpler to change out script that's used.
public $aliases = [
'csrf' => \CodeIgniter\Filters\CSRF::class,
'toolbar' => \CodeIgniter\Filters\DebugToolbar::class,
'honeypot' => \CodeIgniter\Filters\Honeypot::class,
'auth' => \App\Filters\Auth::class,
'authaccess' => \App\Filters\AuthAccess::class
];
// Always applied before every request
public $globals = [
'before' => [
//'honeypot'
'csrf' => ['except' => [
'api/ApiManageMaintenance/delete'
]
]
],
'after' => [
'toolbar',
//'honeypot'
],
];
// Works on all of a particular HTTP method
// (GET, POST, etc) as BEFORE filters only
// like: 'post' => ['CSRF', 'throttle'],
public $methods = [
];
// List filter aliases and any before/after uri patterns
// that they should run on, like:
// 'isLoggedIn' => ['before' => ['account/*', 'profiles/*']],
public $filters = [];
}
注意:我使用的是第三方数据库库=〉Medoo,所以忽略它。由于某些原因,我没有在codeigniter中使用内置的框架数据库查询,因为Medoo对我来说看起来很轻很简单。
则For正在工作:
- 可休息的API:创建=〉工作=〉使用Postman方法测试:后处理
- 可休息的API:show / index =〉工作中=〉 Postman 法测试:获取
- https://somedomain.id/index.php/ApiManageMaintenance
如果不工作: - 可休息的API:更新=〉不工作=〉使用Postman方法测试:放
- 可休息的API:删除=〉不工作=〉使用 Postman 方法测试:删除
- https://somedomain.id/index.php/ApiManageMaintenance/7
在Postman中尝试时,Restful API删除和更新都给予错误:
403 -禁止访问:访问被拒绝。您无权使用您提供的凭据查看此目录或页面。
我还在config =〉filter.php中添加了例外
public $globals = [
'before' => [
//'honeypot'
'csrf' => ['except' => [
'api/ApiManageMaintenance/delete'
]
]
],
];
我不是很理解config filter.php,但看起来这行代码将使api删除工作。
'csrf' => ['except' => [
'api/ApiManageMaintenance/delete'
]
]
"现在我的问题是"
1.是否有任何特定的设置或配置,我错过或我需要做的Restfu API,使API Restfull工作?
任何来自这个社区的帮助都是非常感谢的。
3条答案
按热度按时间dxxyhpgq1#
答案:
在Codeigniter 4中的文件夹Filters中创建文件筛选器
输入此代码:
主要代码为:
然后在configFilters.php中
放置和添加别名此代码:
备注:
我使用过滤器名称FilterBasicAuth。您可以更改为您的名称,并确保在别名中也更改名称。
仅此而已。
kqlmhetl2#
好吧,最好的方法是implelemnet restfull apici4
API ctl
组ctl
实体
服务负责人
jq6vz3qz3#
这是第2部分
模型化
第一次