Codeigniter URI类如何使用 - hyphen而不是_ underscore? [英] Codeigniter URI Class how can i use – hyphen instead _ underscore?

查看:168
本文介绍了Codeigniter URI类如何使用 - hyphen而不是_ underscore?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



我会这样:
http:// localhost:8888 / ci / index.php / get-artist-discography / artist-name



我的代码:



/ controllers:

 <?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);


}

}

/ models:

 <?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;
}

}
}


方案

是的,可以。



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



由于您知道控制器名称和方法名称不能包含 - (连字符),因此您不能更改其名称。



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



像你可以在你的config / routes.php

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

这将执行 get_artist_discography code> artist_name 方法(如果您的链接 http:// localhost:8888 / ci / index.php / get-artist-discography / artist-name



您可以详细了解 URI路由在CI文档


i tried create some controllers, models and views with - hyphen, but i get php error always so i am using underscore right now.

so my url is: http://localhost:8888/ci/index.php/get_artist_discography/artist_name

i would like be like that: http://localhost:8888/ci/index.php/get-artist-discography/artist-name

its possible have urls with - hyphen in codeigniter?

my code:

/controllers:

    <?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);


    }

}

/models:

  <?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;
        }

    }
}

解决方案

Yes you can.

Normally CI produce url like this base_url/Controller_name/Method_name.

As you know controller name and method name cannot contain '-'(hyphen) so you cannot change their name.

What can you do is use router to show the correct controller with corresponding url.

Like you can write this code at your config/routes.php

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

This will execute your get_artist_discography controller and artist_name method if your link is http://localhost:8888/ci/index.php/get-artist-discography/artist-name

You can learn more about URI Routing at CI docs

这篇关于Codeigniter URI类如何使用 - hyphen而不是_ underscore?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆