路由未找到 404 CodeIgniter3

huangapple go评论59阅读模式
英文:

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[&#39;contato&#39;] = &#39;/contato/index&#39;;
$route[&#39;contato/(:num)&#39;][&#39;GET&#39;] = &#39;/contato/index/$1&#39;;

and the function should look like this:

public function index($id = false) {
    $data = &#39;&#39;;
    
    if (is_numeric($id)) {
        $data = $this-&gt;contato_model-&gt;get($id);
    } else {
        $data = $this-&gt;contato_model-&gt;get();
    }

    echo $this-&gt;ultils-&gt;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”):

&lt;?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

huangapple
  • 本文由 发表于 2023年2月23日 21:16:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75545358.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定