Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter URI Class how can i use – hyphen instead _ underscore?

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

    }
}
like image 783
raduken Avatar asked Jun 07 '15 10:06

raduken


2 Answers

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

$route['translate_uri_dashes'] = TRUE;
like image 89
Didin Sino Avatar answered Nov 01 '22 14:11

Didin Sino


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

like image 25
Shaiful Islam Avatar answered Nov 01 '22 16:11

Shaiful Islam