我有以下错误:
未找到404页面
找不到您请求的页面。
以下是我的代码文件。
控制器代码:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
*
*/
class Pages extends CI_Controller
{
public function view($page='home')
{
# code...
if (!file_exists(APPPATH.'views/ds_pages/'.$page.'.php')) {
# code...
show_404();
}
$data['title']=ucfirst($page);
$data['portfolios']= $this->page_model->get_portfolios(FALSE);
$this->load->view('templates/header');
$this->load->view('ds_pages/'.$page,$data);
$this->load->view('templates/footer');
}
public function portfolio_view($slug= NULL)
{
# code...
$data['portfolio']=$this->page_model->get_portfolios($slug);
if (empty($data['portfolio'])) {
# code...
show_404();
}
$data['title']= $data['portfolio']['title'];
$this->load->view('templates/header');
$this->load->view('ds_pages/portfolio_view',$data);
$this->load->view('templates/footer');
}
public function create(){
# code...
$data['title']= 'Create Portfolio';
$this->form_validation->set_rules('title','Title','required');
$this->form_validation->set_rules('body','Body','required');
if($this->form_validation->run() === FALSE){
$this->load->view('templates/header');
$this->load->view('ds_pages/create',$data);
$this->load->view('templates/footer');
}else{
$config['upload_path']= './assets/images/pages';
$config['allowed_types']= 'gif|jpg|png';
$config['max_size']= '2048';
$config['max_width']= '2000';
$config['max_height']= '2000';
$this->load->library('upload',$config);
if (!$this->upload->do_upload()) {
# code...
$errors=array('error'=> $this->upload->display_errors());
$portfolio_image='placeholder.png';
}else{
$data=array('upload_data' => $this->upload->data());
$portfolio_image= $_FILES['userfile']['name'];
}
$this->page_model->create_portfolio($portfolio_image);
//set message
$this->session->set_flashdata('portfolio_created','Your portfolio has been created');
redirect('pages/view');
}
}
}
?>
然后在Model I中有以下代码:
<?php
/**
*
*/
class Page_model extends CI_Model
{
public function __construct()
{
# code...
$this->load->database();
}
public function get_portfolios($slug=FALSE){
# code...
if ($slug === FALSE) {
# code...
$query= $this->db->get('portfolios');
return $query->result_array();
}
$query= $this->db->get_where('portfolios', array('slug' => $slug));
return $query->row_array();
}
public function create_portfolio($portfolio_image)
{
# code...
$slug= url_title($this->input->post('title'));
$data= array(
'title' => $this->input->post('title'),
'slug'=>$slug,
'body'=>$this->input->post('body'),
'portfolio_image'=> $portfolio_image
);
return $this->db->insert('portfolios',$data);
}
}
?>
这是我的观点:
<small class="post-date"><?php echo $portfolio['created_at']; ?></small><br/>
<div class="row">
<div class="col-md-3">
<img src="<?php echo site_url();?>assets/images/pages/<?php echo $portfolio['portfolio_image'];?>">
</div>
<div class="col-md-9">
<h2><?php echo $portfolio['title']; ?></h2>
<div class="post-body">
<?php echo $portfolio['body']; ?>
</div>
</div>
</div>
<hr/>
<a class="btn btn-info pull-right" href="edit/<?php echo $portfolio['slug']; ?>">Edit</a>
<?php echo form_open('/pages/delete/'.$portfolio['id']);?>
<input type="submit" name="delete" class="btn btn-danger" value="Delete" />
</form>
最后,这段代码如下:routes.php:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$route['pages/view']='pages/view';
$route['pages/create']='pages/create';
$route['pages/(:any)']='pages/portfolio_view/$1';
$route['pages']='pages/view';
$route['default_controller'] = 'pages/view';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
请帮助我,并提前感谢你。
答案 0 :(得分:0)
你应该使用
$ route [&#39; pages /(:any)/(:any)&#39;] =&#34; pages / portfolio_view / $ 1&#34;;
而不是
$路线[&#39;页/(:任何)&#39;] =&#39;页/ portfolio_view / $ 1和#39 ;;
在routes.php中