瑣碎知識點

1.JS和CSS的路徑問題

1.在view中的html靜態頁面引入js或cs時不是從所在的頁面開始找而是從從入口文件index.php開始找的,
2.把全部要引入的js或cs文件所有放在public文件夾下,再把文件引入到當前頁面的時候使用:php

傳統方式的導入外部JS和CSS文件的方法是直接在模板文件使用:html

找路徑是從入口文件index.php來找的ajax

在存放JS和CSS的時候能夠放到public文件夾下sql

2.第三方類的引用

有兩塊地方能夠放第三方類
(1)模塊下面
(2)Library下面新建文件夾或者舊的文件夾裏面
放進去以後,須要添加命名空間,命名空間從根命名空間寫起
注意類文件的命名規則和類名的規則

以前是使用連貫操做:limit數組

而如今是想辦法將以前的分頁類page.class.php引入使用:session

Library下面新建文件夾或者舊的文件夾裏面

作法1:在ThinkPHP\Library\Think文件夾下新建fenye文件夾並將以前的page.class.php複製到fenye裏

page.class.php的命名空間:namespace Think\fenye;(其他不發生變化)函數

控制器裏面寫入:use Think\fenye\Page;ui

複製代碼
<?php
namespace Home\Controller;
use Think\Controller;
use Think\fenye\Page;
class ZhuceController extends Controller
{
    
    public function FenYe()
    {
        $model = D("Nation");
        $total = $model->count();
        
        $page = new Page($total,1);
        
        $sql = "select * from Nation ".$page->limit;
        $attr = $model->query($sql);
        
        $xinxi = $page->fpage();
        $this->assign("xinxi",$xinxi);
        //var_dump($attr);
        $this->assign("nation",$attr);
        $this->display();
    }

}
複製代碼

模板顯示html代碼:FenYe.htmlthis

複製代碼
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>分頁信息顯示</title>
</head>

<body>
<h1>主頁面</h1>
<table width="50%" border="1" cellpadding="0" cellspacing="0">
<tr>
    <td>代號</td>
    <td>名稱</td>
</tr>
<foreach name="nation" item="v">
<tr>
    <td><{$v.code}></td>
    <td><{$v.name}></td>
</tr>
</foreach>
</table>
<div><{$xinxi}></div>
</body>
</html>
複製代碼

顯示效果:url

作法2.在Home模塊下的作法

在Home下新建文件夾fenye而後引入Page.class.php,其他不發生改變,效果是同樣的

分頁類裏面的命名空間發生改變:namespace Home\fenye

控制器裏面的的命名空間改變:use Home\fenye\Page

作法3.不用use的方法改下作法2:

作法4:改變分頁的部分條件使用連貫操做的方法

    

方法的改變:

複製代碼
public function FenYe()
    {
        $model = D("Nation");
        $total = $model->count();
        
        $page = new \Home\fenye\Page($total,1);
        
        //方法2:分頁類發生改變的時候
        $attr = $model->limit($page->limit)->select();
        
        $xinxi = $page->fpage();
        $this->assign("xinxi",$xinxi);
        //var_dump($attr);
        $this->assign("nation",$attr);
        $this->display();
    }
複製代碼

 

3.Ajax返回

(1)url要變爲MVC模式,指向的不是具體頁面,是操做方法

(2)在操做方法裏面返回值的時候,使用ajaxReturn()方法返回,注意返回類型,返回類型要和第二個參數一致

ThinkPHP能夠很好的支持AJAX請求,系統的\Think\Controller類提供了ajaxReturn方法用於AJAX調用後返回數據給客戶端。而且支持JSON、JSONP、XML和EVAL四種方式給客戶端接受數據,而且支持配置其餘方式的數據格式返回。

1)利用Ajax返回的動態驗證-----Zhuce和Yhm的方法:

  View Code

 

2)Zhuce.html

  View Code

 

顯示效果:

    

4.Session和Cookie

系統提供了Session管理和操做的完善支持,所有操做能夠經過一個內置的session函數完成,該函數能夠完成Session的設置、獲取、刪除和管理操做。

session初始化設置

若是session方法的第一個參數傳入數組則表示進行session初始化設置,例如:

關閉自動啓動後能夠項目的公共文件或者在控制器中經過手動調用session_start或者session('[start]')啓動session。

登陸:作法1:特別注意命名空間

LoginController.class.php

  View Code

Login.html

  View Code

登陸:作法2:注意命名空間,比以前的作法要方便(不論訪問哪一個都跳轉到登陸)

CheckController.class.php

複製代碼
<?php
namespace Home\Controller;
use Think\Controller;
class CheckController extends Controller
{
    //這個類要做爲全部控制器的父類
        function __construct()
        {
            parent::__construct();
            if(!session('?uid'))
            {
                $this->error("未登陸",U("Login/Login"));    
            }
        }    
}
複製代碼

更改MainController.class.php裏面的命名空間:

namespace Home\Controller;
use Home\Controller\CheckController;
class MainController extends CheckController

這是不論訪問MainController下的哪一個路徑都只能到登陸

 

 

登陸的LoginController.class.php命名空間試過兩種都是能夠的:(與作法1是同樣的)

1)

namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller

2)

namespace Home\Controller;
use Home\Controller\CheckController;
class LoginController extends CheckController

改變

 

5.防止用戶繞過登陸直接訪問操做方法
作一個控制器的父類,在該父類裏面寫一個構造方法,構造方法裏面判斷session裏面有沒有值,若是沒值。。。若是有值。。。

相關文章
相關標籤/搜索