Codeigniter URI Class how can i use – hyphen inste

2019-06-18 03:53发布

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

    }
}

2条回答
我命由我不由天
2楼-- · 2019-06-18 04:11

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

查看更多
欢心
3楼-- · 2019-06-18 04:35

if you're using Codeigniter 3 open your config/routes.php

$route['translate_uri_dashes'] = TRUE;
查看更多
登录 后发表回答