Codeigniter URI Class, how can I use - hyphen instead of _ underscore?

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

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

I would like this: http: // localhost: 8888 / ci / index.php / get-artist-discography / artist-name

its possible have urls with a hyphen in the encoder?

my code is:

/ 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; } } } 
+6
source share
2 answers

Yes, you can.

Typically, CI creates a url like this base_url / controller_name / method name.

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

What you can do is use a router to display the correct controller with the appropriate URL.

How can you write this code in config / routes.php

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

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

You can learn more about URI routing in CI docs.

+10
source

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

 $route['translate_uri_dashes'] = TRUE; 
+10
source

Source: https://habr.com/ru/post/988641/


All Articles