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;
}
}
}
if you're using Codeigniter 3 open your config/routes.php
$route['translate_uri_dashes'] = TRUE;
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With