控制器: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);
}
}
视图:
<a href="<?php echo base_url(); ?>study-abroad">Study Abroad</a>
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";
在这段代码中,我创建了一个名为Test.php的控制器,我在其中加载了一个名为study-study的视图文件。现在,我想删除控制器名称,即从我的URL中删除。我已在路径文件中定义了国外学习的路径,并从视图文件中删除了控制器名称,但它将我重定向到了错误的页面。如果我将控制器名称放在视图文件之前,它会将实际页面重定向到我。这有什么问题?
答案 0 :(得分:1)
<?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;
如上所述,$route['(:any)']
将匹配任何网址,因此请将其他自定义路由放在其他路线之前
在视图中调用路线:
<a href="<?= base_url('study-abroad') ?>">Study Abroad</a>
希望它有所帮助。
答案 1 :(得分:0)
您可以在Code Igniter中为每个网址设置路线。
在config/routes.php
文件中,只需按照以下方式设置每个页面:
$route['study-abroad'] = "test/study_abroad";
希望它可以帮到你