how to modify routes in codeigniter

2019-05-09 22:43发布

问题:

help me with this.

Take example: I have this normal url "localhost/CI/index.php/base/storeurl".

How can I let Codeigniter know to look for "localhost/CI/storeurl".

I have a function named index and it accepts a parameter storeURL in the Base.php class. Help me with this. Thanks in advance.

回答1:

I've finally found what I was looking for. Here is how my code looks like in routes.php.

/* Custom Routes. */
// Store Normal Pages.
$route['home/(:any)'] = "base/home/$1";
$route['about/(:any)'] = "base/about/$1";
$route['services/(:any)'] = "base/services/$1";
$route['contact/(:any)'] = "base/contact/$1";
$route['feedback/(:any)'] = "base/feedback/$1";

// CategoryPage.
$route['category/(:any)/(:num)'] = "base/category/$1/$2";
// ProductPage.
$route['product/(:any)/(:num)'] = "base/product/$1/$2";


// For Main Home Page.
$route['(:any)'] = "base/home/$1";

I really Appreciate everyone who helped me solving this problem. Thank You Guys.



回答2:

Only three steps are require to remove index.php from url in CodeIgniter in WAMP environment.

1) Create .htaccess file in parallel to application holder and just copy past the following code:

RewriteEngine On
RewriteBase /CodeIgniter/
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

2) Change $config['index_page'] to blank in config.php in application folder as below:

$config['index_page'] = '';

3) Enable rewrite_module of apache.

You can also refer to http://goo.gl/3jfW8f



回答3:

Here is some pseudo code and a few points in the right direction to get you started.

Firstly, you will need to remove the index.php from your urls. There must be hundreds of questions on here already answering this part of the question, so I will leave this part up to you.

As many have commented, there is no way to achieve this url structure out the box, and it will require quite a lot of code to get working. The way I would implement this particular setup would be to route all requests to a single controller, and handle custom routing within that controller.

To do this add a route to the bottom of the routes.php as a catch all. Routes for other controllers (news, events, etc) can then be added above this one. Here is an example routes.php to get you started.

config/routes.php

// Other routes go up here

// Catch all route 
$route['(:any)'] = 'page/view';

// Home page route 
$route['default_controller'] = "page/index";

You could then create a view function in controllers/page.php that will check the url (using codeigniters url helper), and load the correct view corresponding to that url.

controllers/page.php

class Page extends CI_Controller {

   public function index()
   {    
      // Do all code for your home page in here
   }

   public function view()
   {        
      // Retrive the url string
      $url = $this->uri->uri_string();

      // Check if the corresponding view file exists
      if (file_exists(APPPATH.'views/'.$url.'/index.php')){

         // Load the view
         $this->load->view($url.'/index');

      } else {
         show_404();
      } 
   }
}

Say for example, a user went to http://yoursite.com/CI/about-us, the page controllers view function would take the string about-us from the url and set it as the $url variable, and then search your file structure to check if the corresponding view file exists /application/views/about-us/index.php. If it does, it will then load that view file, if it doesn't it will redirect to a 404.

The above is all pseudo code typed from memory, so it probably won't work straight off, but hopefully give you a clue as to how to achieve your desired result.



回答4:

I always cant get routing to work. finally I found the solution. I just want to share to help people in need as I really like codeigniter as it is speedy and light. I am working on dynamic category title to display on $this->uri->segment(1).

$route['(:any)/method/(:any)']  = 'Class/method';    
$route['(:any)/gallery/(:any)'] = 'Class/gallery';    
$route['(:any)/listing/(:any)'] = 'Class/listing';

The route will note that as long as $this->uri->segment(2) is the method, it will go to execute the Class's method. Your url will be beautiful:

base_url()/business-services/gallery/6 
base_url()/travel/gallery/12

i also learned that route have to be specific. if url is like:

base_url()/travel/singapore/listing/1201

Then your route have to be:

$route['(:any)/(:any)/listing/(:num)']  = 'Class/listing';

if you have param to pass in method(depend on if you use param or uri_segment),

$route['(:any)/(:any)/listing/(:num)']  = 'Class/listing/$1';

have fun :)