首页 技术 正文
技术 2022年11月15日
0 收藏 677 点赞 3,770 浏览 2423 个字

我们来看一下CI4框架的默认页面是如何实现的。

我们先来认识一下路由文件(app\Config\Routes.php),这个文件非常重要,且功能强大,它定义了URL模式及响应处理方法,我们先看一下这个文件的部分内容。

 /**
* --------------------------------------------------------------------
* Router Setup
* --------------------------------------------------------------------
*/
$routes->setDefaultNamespace('App\Controllers'); // 设置默认的命名空间
$routes->setDefaultController('Home'); // 设置默认的控制器
$routes->setDefaultMethod('index'); // 设置默认的方法
$routes->setTranslateURIDashes(false);
$routes->set404Override();
$routes->setAutoRoute(true); /**
* --------------------------------------------------------------------
* Route Definitions
* --------------------------------------------------------------------
*/ // We get a performance increase by specifying the default
// route since we don't have to scan directories.
$routes->get('/', 'Home::index'); // 设置访问网站根目录时通过Home控制器的index方法响应

我们可以看到上面代码通过2种方式定义了默认页面

1. 定义默认控制器为Home,默认方法为index

2. 定义路由匹配 “/” 时通过控制器Home的index方法响应

思考一下,如果以上两种方法定义的不一致,哪个优先级更高呢?我们来修改一下代码

 /**
* --------------------------------------------------------------------
* Router Setup
* --------------------------------------------------------------------
*/
$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override();
$routes->setAutoRoute(true); /**
* --------------------------------------------------------------------
* Route Definitions
* --------------------------------------------------------------------
*/ // We get a performance increase by specifying the default
// route since we don't have to scan directories.
$routes->get('/', function () {
echo '第一个页面';
});

刷新页面,发现输出 “第一个页面”,证明定义路由的优先级更高。

我们再来看一下Home控制器(app\Controllers\Home.php)

 <?php namespace App\Controllers; class Home extends BaseController
{
public function index()
{
return view('welcome_message');
}
}

里面只有一个index方法,返回了一个视图模板welcome_message,我们再来添加一个方法

 <?php namespace App\Controllers; class Home extends BaseController
{
public function index()
{
return view('welcome_message');
} public function welcome()
{
echo 'welcome to codeigniter 4!';
}
}

我们来访问一下 /home/welcome, 看下页面效果

我们再添加一条路由规则

 /**
* --------------------------------------------------------------------
* Route Definitions
* --------------------------------------------------------------------
*/ // We get a performance increase by specifying the default
// route since we don't have to scan directories.
$routes->get('/', function () {
echo '第一个页面';
});
$routes->get('/welcome.html', 'Home::welcome');

我们再访问一下 /welcome.html, 可以看到一样的页面内容, 一不小心实现了页面静态化呢!

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,413
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,186
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905