【问题标题】:Codeigniter URI Class how can i use – hyphen instead _ underscore?Codeigniter URI 类我如何使用 - 连字符而不是 _ 下划线?
【发布时间】:2015-06-07 10:04:42
【问题描述】:

我尝试使用 - 连字符创建一些控制器、模型和视图,但我总是收到 php 错误,所以我现在使用下划线。

所以我的网址是: http://localhost:8888/ci/index.php/get_artist_discography/artist_name

我希望是这样的: http://localhost:8888/ci/index.php/get-artist-discography/artist-name

codeigniter 中可能有带有 - 连字符的 url?

我的代码:

/控制器:

    <?php 
include (APPPATH.'/libraries/REST_Controller.php');
class get_artist_discography extends REST_Controller {

    function artist_name_get(){

    $data = new stdClass();
    $this->load->model('artist_model');
    $data = $this->artist_model->getAll();$this->response($data, 200);


    }

}

/模型:

  <?php 
class artist_model extends CI_Model {
    function getAll(){

        $q  = $this->db->query("SELECT artist_discography,artist_name from music");

        if($q->num_rows() > 0) {

            foreach ($q->result() as $row) {
                $data [] = $row;
            }
            return $data;
        }

    }
}

【问题讨论】:

    标签: php codeigniter url


    【解决方案1】:

    如果您使用 Codeigniter 3,请打开您的 config/routes.php

    $route['translate_uri_dashes'] = TRUE;
    

    【讨论】:

    • 是的,如果您的控制器和方法名称与 URL 相同,只有连字符和下划线不同,并且您使用 CI-3,您可以使用此解决方案。Details here
    【解决方案2】:

    是的,你可以。

    通常 CI 会生成类似 base_url/Controller_name/Method_name 的 url。

    如您所知,控制器名称和方法名称不能包含“-”(连字符),因此您不能更改它们的名称。

    你可以做的是使用路由器显示正确的控制器和相应的 url。

    就像您可以在 config/routes.php 中编写此代码一样

    $route['get-artist-discography/artist-name'] ='get_artist_discography/artist_name';
    

    如果您的链接是http://localhost:8888/ci/index.php/get-artist-discography/artist-name,这将执行您的get_artist_discography 控制器和artist_name 方法

    您可以了解更多关于URI Routing at CI docs

    【讨论】:

    • 使用 $route['translate_uri_dashes'] = TRUE;在 config/routes.php 中。您不必为每个控制器添加路由。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多