简体   繁体   English

如何将参数传递给索引和自定义路由

[英]how to passing parameter to index and custom route

I want to pass parameters to index function and if the parameter is not, redirect to home page. 我想将参数传递给索引函数,如果不是,则重定向到主页。 i added custom route also. 我还添加了自定义路线。 where did i miss the code? 我在哪里错过代码?

$route['main/(:any)'] = 'main/index/'; // looks like has a mistake

www.example.com ->when deafult index function www.example.com->当默认索引功能时

www.example.com/females ->when deafult index has string parameter 'female' www.example.com/females->当默认索引具有字符串参数“ female”时

class Main extends CI_Controller {

public function index($gender) {  

  load->view('inc/header_view');

  if((isset($gender)) && ($gender =='female')){ 

    $this->load->view('female_view');

  } else {

    $this->load->view('female_view');
  }
}

You can achieve this by following code 您可以通过以下代码来实现

class Main extends CI_Controller {

public function index($gender="") {  

  if($gender == "")
  {
     $this->load->view('home');

  } else {

    $this->load->view('female_view');
  }
}

Also as you mentioned to get this URL to work www.example.com & www.example.com/females , you need to add following into your routes.php 另外,正如您提到的www.example.com此URL能够正常运行www.example.comwww.example.com/females ,您还需要在routes.php中添加以下内容

$route['default_controller'] = "main";
$route['(:any)'] = 'main/index/$1';

Let me know in case of any queries 如有任何疑问,请通知我

You should try it like 你应该尝试一下

$route['main'] = 'main/index'; 

$route['main/(:any)'] = 'main/index/$1'; 

Or you can use uri segments. 或者,您可以使用uri细分。

http://www.codeigniter.com/user_guide/libraries/uri.html?highlight=uri%20segments#CI_URI::segment http://www.codeigniter.com/user_guide/libraries/uri.html?highlight=uri%20segments#CI_URI::segment

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM