[英]How to remove controller from url in codeigniter?
controller:Test.php 控制器:test.php的
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends CI_Controller
{
function __construct()
{
parent :: __construct();
$this->load->helper(array('form', 'url', 'captcha', 'email', 'html'));
}
public function study_abroad()
{
$data['student_id'] = $this->session->userdata('student_id');
$this->load->view('study-abroad',$data);
}
}
view: 视图:
<a href="<?php echo base_url(); ?>study-abroad">Study Abroad</a>
route.php route.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$route['default_controller'] = 'test';
$route['404_override'] = '';
$route['translate_uri_dashes'] = TRUE;
$route['(:any)'] = "colleges/index/$1";
$route['college/(:any)'] = "test/college/$1";
$route['study-abroad'] = "test/study_abroad";
In this code, I have created a controller having name Test.php where I have load a view file having name study-abroad. 在此代码中,我创建了一个名称为Test.php的控制器,并在其中加载了名称为study-abroad的视图文件。 Now, I want to remove controller name ie Test from my URL. 现在,我想从我的URL中删除控制器名称,即Test。 I have defined the path of study abroad in my route file and delete controller name from view file but it redirects me to the wrong page. 我已经在路线文件中定义了出国留学的路径,并从视图文件中删除了控制器名称,但它会将我重定向到错误的页面。 If I put controller name before view file it will redirect me the actual page. 如果我将控制器名称放在视图文件之前,它将重定向我实际的页面。 What is the problem behind this? 这是什么问题呢?
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$route['college/(:any)'] = "test/college/$1";
$route['study-abroad'] = "test/study_abroad";
$route['(:any)'] = "colleges/index/$1";
$route['default_controller'] = 'test';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
As indicated, $route['(:any)']
will match any URL, so place your other custom routes before the other route route 如所示, $route['(:any)']
将匹配任何URL,因此将其他自定义路由放在其他路由之前
Calling a route in view: 在视图中调用路线:
<a href="<?= base_url('study-abroad') ?>">Study Abroad</a>
Hope It Helps. 希望能帮助到你。
You can set your a route for each url in Code Igniter. 您可以在Code Igniter中为每个网址设置路由。 In your config/routes.php
file, just set each page like this: 在您的config/routes.php
文件中,像这样设置每个页面:
$route['study-abroad'] = "test/study_abroad";
Hope it helps you 希望对您有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.