gpt4 book ai didi

php - Codeigniter 4级路由不起作用

转载 作者:行者123 更新时间:2023-12-04 18:35:56 37 4
gpt4 key购买 nike

routes.php

$ route ['admin/news'] ='admin_news/index';//在职的
$ route ['admin/news/(:any)'] ='admin_news/view/$ 1';//在职的
$ route ['admin/news/create'] ='admin_news/create';//在职的
$ route ['admin/news/edit/(:any)'] ='admin_news/edit/$ 1';//不工作
$ route ['admin/news/delete/(:any)'] ='admin_news/delete/$ 1';//不工作

Controller :admin_news.php

如果(!defined('BASEPATH'))
exit('不允许直接脚本访问');

Admin_news类扩展了CI_Controller {

公共(public)功能__construct()
{
parent :: __ construct();
$ this-> load-> model('news_model');
$ this-> load-> helper('url');

if(!$ this-> session-> userdata('is_logged_in')){
redirect('admin/login');
}
}

公共(public)功能index()
{
$ data ['news'] = $ this-> news_model-> get_news();
$ data ['title'] ='新闻文件';

$ this-> load-> view('admin/includes/header',$ data);
$ this-> load-> view('admin/news/index',$ data);
$ this-> load-> view('admin/includes/footer');
}

公共(public)功能 View ($ slug = NULL)
{
$ data ['news_item'] = $ this-> news_model-> get_news($ slug);

如果(空($ data ['news_item']))
{
show_404();
}

$ data ['title'] = $ data ['news_item'] ['title'];

//$ this-> load-> view('templates/header',$ data);
$ this-> load-> view('admin/news/view',$ data);
//$ this-> load-> view('templates/footer');
}

公共(public)功能create()
{
$ this-> load-> helper('form');
$ this-> load-> library('form_validation');

$ data ['title'] ='创建新闻';

$ this-> form_validation-> set_rules('title','Title','required');
$ this-> form_validation-> set_rules('text','Text','required');

如果($ this-> form_validation-> run()=== FALSE)
{
$ this-> load-> view('admin/includes/header',$ data);
$ this-> load-> view('admin/news/create',$ data);
$ this-> load-> view('admin/includes/footer');

}
别的
{
$ this-> news_model-> set_news();
$ this-> load-> helper('url');
$ this-> index();
}
}

公共(public)功能编辑($ slug)
{
$ data ['news_item'] = $ this-> news_model-> get_news($ slug);

如果(空($ data ['news_item']))
{
show_404();
}

$ data ['title'] ='编辑:'。$ data ['news_item'] ['title'];

$ this-> load-> helper('form');
$ this-> load-> library('form_validation');

$ this-> form_validation-> set_rules('title','title','required');
$ this-> form_validation-> set_rules('text','text','required');

if($ this-> form_validation-> run()===否)
{
$ this-> load-> view('admin/includes/header',$ data);
$ this-> load-> view('admin/news/edit',$ data);
$ this-> load-> view('admin/includes/footer');
}
别的
{
$ this-> news_model-> update_news($ this-> input-> post('id'),
$ this-> input-> post('title'),
$ this-> input-> post('text'));

$ data ['news_item'] = $ this-> news_model-> get_news($ slug);
$ this-> load-> view('admin/includes/header',$ data);
$ this-> load-> view('admin/news/success');
$ this-> load-> view('admin/news/edit',$ data);
$ this-> load-> view('admin/includes/footer');
}
}

公共(public)功能delete($ id = NULL){
$ this-> news_model-> delete_news($ id);
$ this-> load-> helper('url');
$ this-> index();
}

}

模型:News_model.php

News_model类扩展了CI_Model {

公共(public)功能__construct()
{
$ this-> load-> database();
}

公共(public)功能get_news($ slug = FALSE)
{
如果($ slug === FALSE)
{
$ query = $ this-> db-> get('news');
返回$ query-> result_array();
}

$ query = $ this-> db-> get_where('news',array('slug'=> $ slug));
返回$ query-> row_array();
}

公共(public)功能set_news()
{
$ this-> load-> helper('url');

$ slug = url_title($ this-> input-> post('title'),'dash',TRUE);

$数据=数组(
'标题'=> $ this-> input-> post('title'),
'slug'=> $ slug,
'文本'=> $ this-> input-> post('text')
);

返回$ this-> db-> insert('news',$ data);
}

/*公共(public)函数update_news($ slug = FALSE)
{
$ this-> load-> helper('url');

$ slug = url_title($ this-> input-> post('title'),'dash',TRUE);

$数据=数组(
'slug'=> $ slug,
'标题'=> $ this-> input-> post('title'),
'文本'=> $ this-> input-> post('text')
);
$ this-> db-> where('slug',$ slug);
返回$ this-> db-> update('news',$ data);
} */

公共(public)功能update_news($ id,$ title,$ text){
$数据=数组(
'title'=> $ title,
'文本'=> $ text
);

$ this-> db-> where('id',$ id);
$ this-> db-> update('news',$ data);
}

公共(public)功能delete_news($ id = FALSE)
{
$ this-> db-> delete('news',array('id'=> $ id));
}
}

View :admin/news/edit.php

编辑新闻项


<?php echo validate_errors(); ?>

<?php echo form_open('news/edit/'.$ news_item ['slug'])?>


标题
<?php echo form_input('title',$ news_item ['title']); ?>



文本
<?php echo form_textarea('text',$ news_item ['text']);; ?>


<?php echo form_hidden('slug',$ news_item ['slug']);; ?>
<?php echo form_hidden('id',$ news_item ['id']); ?>


<?php echo form_submit('submit','Save Changes'); ?>


<?php echo form_close(); ?>

当我访问`http://localhost/ciadmin/admin/news/edit/news-slug`时,显示 404页面未找到消息!

最佳答案

这样说:

$route['admin/news/delete/(:any)'] = 'admin_news/delete/$1';
$route['admin/news/edit/(:any)'] = 'admin_news/edit/$1';
$route['admin/news/create'] = 'admin_news/create';
$route['admin/news/(:any)'] = 'admin_news/view/$1';
$route['admin/news'] = 'admin_news/index';

记住:

Routes will run in the order they are defined. Higher routes will always take precedence over lower ones.



Docs

关于php - Codeigniter 4级路由不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34843217/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com