英文:
route not found 404 codeigniter3
问题
我有一个应用程序,其中包含一些路由,但在包含参数时出现错误。
$route['contato'] = '/contato';
$route['contato/(:num)']['GET'] = '/contato/$1';
$route['contato/(:num)']['PUT'] = 'contato/update/$1';
$route['contato/(:num)']['DELETE'] = 'contato/delete/$1';
控制器:
public function index()
{
$data = '';
$id = $this->input->get('id');
if (isset($id)) {
$data = $this->contato_model->get($id);
} else {
$data = $this->contato_model->get();
}
echo $this->ultils->toJson($data);
}
当通过URL访问API https://localhost/api/contact/1
时出现404错误。
只返回一个记录,如果我将代码更改为 ?=id
并将其传递到URL contato?id=2
,它可以正常工作,我需要在 /contato/2
路由中使用这种方式。
英文:
I have an app with some routes, but I'm having an error when including a parameter
$route['contato'] = '/contato';
$route['contato/(:num)']['GET'] = '/contato/$1';
$route['contato/(:num)']['PUT'] = 'contato/update/$1';
$route['contato/(:num)']['DELETE'] = 'contato/delete/$1';
Controller
public function index()
{
$data = '';
$id = $this->input->get('id')
if (isset($id)) {
$data = $this->contato_model->get($id);
} else {
$data = $this->contato_model->get();
}
echo $this->ultils->toJson($data);
}
More when accessing the api https://localhost/api/contact/1
via url
Give error not found (404)
Return of just one record, if I change the code to ?=id and pass it in the url contato?id=2 it works, I need it to be that way in the /contato/2 route
答案1
得分: 1
在config/routes.php
文件中,您需要指定类内的哪个函数调用:
$route['contato'] = '/contato/index';
$route['contato/(:num)']['GET'] = '/contato/index/$1';
函数应该如下所示:
public function index($id = false) {
$data = '';
if (is_numeric($id)) {
$data = $this->contato_model->get($id);
} else {
$data = $this->contato_model->get();
}
echo $this->ultils->toJson($data);
}
将URI分段传递给您的方法
如果您的URI包含超过两个段,它们将作为参数传递给您的方法。
例如,假设您有一个像这样的URI:
example.com/index.php/products/shoes/sandals/123
您的方法将传递URI段3和4(“sandals”和“123”):
<?php
class Products extends CI_Controller {
public function shoes($category, $id) {
echo $category;
echo $id;
}
}
来源:https://codeigniter.com/userguide3/general/controllers.html#passing-uri-segments-to-your-methods
英文:
in config/routes.php
you need to specify which function call within the class:
$route['contato'] = '/contato/index';
$route['contato/(:num)']['GET'] = '/contato/index/$1';
and the function should look like this:
public function index($id = false) {
$data = '';
if (is_numeric($id)) {
$data = $this->contato_model->get($id);
} else {
$data = $this->contato_model->get();
}
echo $this->ultils->toJson($data);
}
Passing URI Segments to your methods
If your URI contains more than two segments they will be passed to your method as parameters.
For example, let’s say you have a URI like this:
example.com/index.php/products/shoes/sandals/123
Your method will be passed URI segments 3 and 4 (“sandals” and “123”):
<?php
class Products extends CI_Controller {
public function shoes($category, $id) {
echo $category;
echo $id;
}
}
https://codeigniter.com/userguide3/general/controllers.html#passing-uri-segments-to-your-methods
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论