在codeigniter中定义正确的路径 [英] Define correct path in codeigniter
本文介绍了在codeigniter中定义正确的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在插入数据,我将删除并稍后在同一页面上更新。我的路径和路径有问题,(基本网址)视图和控制器。我得到页面404说找不到页面。< br $> b $ b
我尝试过:
是控制器
<?php
class Main扩展CI_Controller {
function __construct()
{
parent: :__构造();
$ this-> load-> helper('url');
$ this-> load-> helper('form');
$ this-> load-> database();
// $ this-> load-> model('Stud_Model');
// $ this-> load-> database();
}
公共函数索引()
{
$ this-> load-> model('main_model');
$ this-> load-> view('main_view',$ data);
}
公共函数form_validation()
{
$ this-> load-> library('form_validation');
$ this-> form_validation-> set_rules('first_name',First Name,'required | alpha');
$ this-> form_validation-> set_rules('last_name',姓氏,'required | alpha');
if($ this-> form_validation-> run())
{// true
$ this-> load-> model('main_model');
$ data = array
(
first_name=> $ this-> input-> post('first_name'),
last_name=> $ this-> input-> post('last_name')
);
$ this-> main_model-> insert_data($ data);
redirect(base_url()。main / inserted);
}
else
{
$ this-> index();
}
}
公共函数inserted()
{
$ this-> index();
}
}
这是视图
<!DOCTYPE html>
< html lang =en>
< head>
< meta charset =utf-8>
< title>将数据插入数据库< / title>
< link rel =stylesheettype =text / csshref =https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css>
< / head>
< body>
< div class =container>
< br />< br />< br />
< h3 align =center>插入数据< / h3>< br />
< form method =postaction =<?php echo base_url();?> main / form_validation>
<?php
if($ this-> uri-> segment(2)==inserted)
{
echo'< p class = text-success> Data Inserted< / p>';
}
?>
< div class =form-group>
< label>输入名字< / label>
< input type =textname =first_nameclass =form-control/>
< span class =text-danger><?php echo form_error('first_name');?>< / span>
< / div>
< div class =form-group>
< label>输入姓氏< / label>
< input type =textname =last_nameclass =form-control/>
< span class =text-danger><?php echo form_error('last_name');?>< / span>
< / div>
< br />
< div class =form-group>
< input type =submitname =insertvalue =Insertclass =btn btn-info/>
< / div>
< / form>
< / div>
< / body>
< / html>
这是我的基本网址
$ config ['base_url'] ='http://127.0.0.2:8383/tuts';
这是我的路线配置,main.php是我上面定义的控制器
$ route ['default_controller'] ='main';
$ route ['404_override'] ='';
$ route ['translate_uri_dashes'] = FALSE;
解决方案
this-> load-> helper('url ');
这 - >负载>辅助(' 形式);
这 - >负载>数据库( );
//
I am inserting data which i would delete and update later on the same page.I have a problem with the route and path,(base url) for the views and controllers.i get page 404 saying page cant be found.
What I have tried:
the is controller
<?php class Main extends CI_Controller { function __construct() { parent::__construct(); $this->load->helper('url'); $this->load->helper('form'); $this->load->database(); //$this->load->model('Stud_Model'); //$this->load->database(); } public function index() { $this->load->model('main_model'); $this->load->view('main_view',$data); } public function form_validation() { $this->load->library('form_validation'); $this->form_validation->set_rules('first_name',"First Name", 'required|alpha'); $this->form_validation->set_rules('last_name',"Last Name", 'required|alpha'); if ($this->form_validation->run()) { //true $this->load->model('main_model'); $data = array ( "first_name" =>$this->input->post('first_name'), "last_name" =>$this->input->post('last_name') ); $this->main_model->insert_data($data); redirect (base_url() . "main/inserted"); } else { $this->index(); } } public function inserted() { $this->index(); } }
this is the view
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Insert Data To Database</title> <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> </head> <body> <div class="container"> <br /><br /><br /> <h3 align="center">Insert Data</h3><br /> <form method="post" action="<?php echo base_url(); ?>main/form_validation"> <?php if ($this->uri->segment(2) == "inserted") { echo '<p class="text-success">Data Inserted </p>'; } ?> <div class="form-group"> <label>Enter First Name</label> <input type="text" name="first_name" class="form-control" /> <span class="text-danger"><?php echo form_error('first_name');?></span> </div> <div class="form-group"> <label>Enter Last Name</label> <input type="text" name="last_name" class="form-control" /> <span class="text-danger"><?php echo form_error('last_name');?></span> </div> <br /> <div class="form-group"> <input type="submit" name="insert" value="Insert" class="btn btn-info" /> </div> </form> </div> </body> </html>
this is my base url
$config['base_url'] = 'http://127.0.0.2:8383/tuts';
this is my route configuration,main.php is the controller i defined above
$route['default_controller'] = 'main'; $route['404_override'] = ''; $route['translate_uri_dashes'] = FALSE;
解决方案
this->load->helper('url');
this->load->helper('form');
this->load->database(); //
这篇关于在codeigniter中定义正确的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文