CodeIgniter学习笔记四:CI中的URL相关函数,路由,伪静态,去掉index.php

一、URL相关函数

1、加载url模块

加载url有两种方式:

a、自动加载:在 application/config/autoload.php 中开启 

$autoload['helper'] = array('url');

b、手动加载:

$this -> load -> helper('url');

2、site_url("controller/action")

用于生成完整地址,可用于form的action属性中。

3、base_url

获得网站根目录,是浏览地址(不是物理地址)

示例:

<!DOCTYPE html>
<html>
<head>
    <title>Url Test</title>
</head>
<body>
    <form action="<?php echo site_url('urltest/add'); ?>" method="post">
        <input type="submit" value="Add"/>
    </form>
    <img src="<?php echo base_url(); ?>uploads/1.jpg" alt="" />
</body>""
</html>

二、路由

1、配置文件在application/config/routes.php

2、默认控制器:

$route['default_controller'] = 'welcome';

三、伪静态(自定义后缀)

//正则路由
$route['news/([\d]+)\.html'] = 'article/show/$1';
$route['article/show/([\d]+)\.html'] = 'article/show/$1';

四、去掉index.php

根据不同Web服务器,添加配置。

如果web服务器是apache,先开启rewrite模块,重启apache,然后在网站根目录(入口文件同级)中添加.htaccess文件:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
</IfModule>

即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值