我想在CodeIgniter中使用路由我无法得到我想在这里传递测试控制器的代码 我想要像这个localhost / code / test这样的网址但是我找不到对象
routes.php
$route['test'] = "test/blog";
test.php controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Test extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* @see http://codeigniter.com/user_guide/general/urls.html
*/
public function index()
{
$this->load->view('welcome_message');
}
public function blog()
{
echo "hi";
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
答案 0 :(得分:2)
您要做的是您需要在views
文件夹中准备一个视图页面,例如blog.php
,您需要做的是您需要在其中传递数据并且必须在其中调用视图部分你的控制器就像
test.php
public function blog()
{
$data['my_post'] = "Hello Its my first blog"; //some sort of operations or else likewise
$this->load->view('blog',$data);
}
在blog.php
<h1><?php echo $my_post;?></h1>
答案 1 :(得分:1)
如果你没有在路线中设置默认控制器,那么首先在设置特定路由之后设置它,如此
$route['default_controller'] = "Test";
$route['404_override'] = '';
$route['test'] = 'test/blog';
$route['test/(:any)'] = 'test/blog/$1';
答案 2 :(得分:0)
首先,将默认控制器设置为您的文件。来自Route.php