PHP CI框架学习之路径访问
PHP CI框架学习之路径访问
CI是一个PHP写的框架,使用它写网站非常方便,但是也会也有一些纠结的问题,比如说其中文件的路径访问。
CI严格贯彻MVC思想,因此基于此思想的文件访问也比较严格,controllers控制器是所有view的入口,从URL上是不能直接访问view的,必须通过控制器,也就是说要写控制器的路径,在写代码的时候,路径也是不能随便写的。
一般CSS、JS、图片文件我们会放到items下,比如说我们想在PHP文件中访问某个图片文件calendar-hilite.gif如下图所示:
就可以这样写图片的路径base_url("items/image/calendar/calendar-hilite.gif")。其中,base_url代表http://localhost/网站名/,而items下的文件,在URL中是可以直接访问的。其实localhost代表的位置是可以在控制文件中设置的,只是一个代表。
而有时我们所要的文件并不一定要写到items下,我们也可能要写到view下,或者控制器下,这是就有两种方式:一个是使用base_url,另一个是使用"./"或者"../"这种通用的路径访问形式,举个例子,比如说我要写一个用Ajax实现的某个功能,其代码如下:
[javascript]- $.ajax({
- type:"POST",
- url:"<?php echo site_url("ajax_part/getCommentDetail");?>",
- data:{
- product_id :id,
- comment_first_id:first,
- comment_last_id:last
- } ,
- success:function(data)
- {
- $("#"+id).empty();
- $("#"+id).html(data);
- }
- });
- $.ajax({
- type:"POST",
- url:"../ajax_part/getCommentDetail",
- data:{
- product_id :id,
- comment_first_id:first,
- comment_last_id:last
- } ,
- success:function(data)
- {
- $("#"+id).empty();
- $("#"+id).html(data);
- }
- });
这就是基本的思路,可能不需要说的这么复杂。
ajax_part.php的大致代码如下:
[php]- <?php
- class Ajax_part extends CI_Controller {
- function __construct() {
- parent::__construct ();
- //something
- }
- function getCommentDetail() {
- //something
- }
- }
- ?>
评论暂时关闭