検索
  • サインイン
  • サインアップ
パスワードのリセットが成功しました

興味のあるプロジェクトをフォローし、それらに関する最新ニュースをチェックしてください

ThinkPHP6.0 その他

コレクション 501
リーディング 181513
更新時間 2016-09-11

1. Session

  • Session クラスを使用するには、ファサード メソッド (think\facade\) を使用する必要があります。 Session )

  • の呼び出し 新しいバージョンでは、ネイティブ $_SESSION 配列と session_ で始まるすべての関数の操作はサポートされていません。

#1. 設定ファイル session.php

return [
    // session name
    'name'           => 'PHPSESSID',
    // SESSION_ID的提交变量,解决flash上传跨域
    'var_session_id' => '',
    // 驱动方式 支持file cache
    'type'           => 'file',
    // 存储连接标识 当type使用cache的时候有效
    'store'          => null,
    // 过期时间
    'expire'         => 1440,
    // 前缀
    'prefix'         => '',
];

2. セッションを開く

  • ミドルウェア

    app\middleware.php ファイル

  • \think\middleware\SessionInit::class
3. 設定

  • セッション サポート複数レベルの配列操作

  • Session::set('name','欧阳克');
    // Session数组
    Session::set('admin.name','欧阳克');
    Session::set('admin.uid',1);
4. 読み取り

// 获取全部Session
Session::all();
// 获取Session中name的值
Session::get('name');

5. 削除

Session::delete('name');

6. 値を取得して

Session::pull('name');
# を削除##7. ログイン例

    新しいlogin.phpファイル
  • namespace app\controller;
    use think\facade\View;
    use think\facade\Db;
    use think\facade\Request;
    use think\facade\Session;
    class Login{
        public function index(){
            if(Request::method() == 'POST'){
                $all = Request::param();
                $admin = Db::table('shop_admin')->where('account',$all['account'])->find();
                if(empty($admin)){
                    echo json_encode(['code'=>1,'msg'=>'未找到管理员']);
                    exit;
                }
                if(md5($all['pwd']) != $admin['password']){
                    echo json_encode(['code'=>1,'msg'=>'密码错误']);
                    exit;
                }
                Session::set('uid',$admin['uid']);
                Session::set('account',$admin['account']);
                echo json_encode(['code'=>0,'msg'=>'登陆成功']) ;
            }else{
                $title = '商城';
                View::assign([
                    'title'  => $title
                ]);
                return View::fetch();
            }
        }
    }
    新しいLogin/index.htmlファイル
  • <!DOCTYPE html>
    <html>
    <head>
        <title>登录</title>
        <link rel="stylesheet" type="text/css" href="/static/layui/css/layui.css">
        <script type="text/javascript" src="/static/layui/layui.js"></script>
    </head>
    <body style="background: #1E9FFF">
        <div style="position: absolute; left:50%;top:50%;width: 500px;margin-left: -250px;margin-top: -200px;">
            <div style="background: #ffffff;padding: 20px;border-radius: 4px;box-shadow: 5px 5px 20px #444444;">
                <form class="layui-form">
                    <div class="layui-form-item" style="color:gray;">
                        <h2>{$title}--后台管理系统</h2>
                    </div>
                    <hr>
                    <div class="layui-form-item">
                        <label class="layui-form-label">用户名</label>
                        <div class="layui-input-block">
                            <input type="text" id="account" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">密&nbsp;&nbsp;&nbsp;&nbsp;码</label>
                        <div class="layui-input-block">
                            <input type="password" id="password" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button type="button" class="layui-btn" onclick="dologin()">登录</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
        <script type="text/javascript">
            layui.use(['layer'],function(){
                $ = layui.jquery;
                layer = layui.layer;
                // 用户名控件获取焦点
                $('#account').focus();
                // 回车登录
                $('input').keydown(function(e){
                    if(e.keyCode == 13){
                        dologin();
                    }
                });
            });
            function dologin(){
                var account = $.trim($('#account').val());
                var pwd = $.trim($('#password').val());
                if(account == ''){
                    layer.alert('请输入用户名',{icon:2});
                    return;
                }
                if(pwd == ''){
                    layer.alert('请输入密码',{icon:2});
                    return;
                }
                $.post('/index.php/login/index',{'account':account,'pwd':pwd},function(res){
                    if(res.code>0){
                        layer.alert(res.msg,{icon:2});
                    }else{
                        layer.msg(res.msg);
                        setTimeout(function(){window.location.href = '/index.php/index/index'},1000);
                    }
                },'json');
            }
        </script>
    </body>
    </html>
#index/index.html ファイル
  • ##
    use think\facade\Session;
    public function index(){
        $title = '商城';
        $session = Session::all();
        if(empty($session['uid'])){
            echo '<script type="text/javascript">alert("请登录!");window.location.href = "/index.php/login/index"; </script>';
            exit;
        }
        $login = $session['account'];
        # 左侧菜单
        $menu = Db::table('shop_menu')->where('fid',0)->select();
        $left = $menu->toArray();
        foreach($left as &$left_v){
            $left_v['lists'] = Db::table('shop_menu')->where('fid',$left_v['id'])->select();
        }
        # 右侧列表
        $param = Request::param();
        if(isset($param['status']) && $param['status'] == 1){
            $where['status'] = 1;
        }else if(isset($param['status']) && $param['status'] == 2){
            $where['status'] = 2;
        }else{
            $where = true;
        }
        $p = isset($param['p']) ? $param['p'] : 1;
        $db = new Goods();
        $order = [
            'add_time DESC',
            'id DESC'
        ];
        $right = $db->get_all($where,$order,$p,5);
        View::assign([
            'title'  => $title,
            'login' => $login,
            'left' => $left,
            'right' => $right['data'],
            'count' => $right['count'],
            'p' => $p,
            'status' => isset($param['status']) ? $param['status'] : 0
        ]);
        return View::fetch();
    }

    2. Cookie

#Cookie クラスを使用するには、ファサード メソッド (

think\facade\Cookie

) を使用する必要があります。cookie.php にある
  • # 構成ファイルを呼び出します。構成ディレクトリ内のファイルです。手動の初期化は必要ありません。システムは、呼び出す前に Cookie を自動的に初期化します。Work

  • 1. Cookie の使用

    // 设置Cookie 有效期为 3600秒
    Cookie::set('name', '欧阳克', 3600);
    // 永久保存Cookie
    Cookie::forever('name', '欧阳克');
    //删除cookie
    Cookie::delete('name');
    // 读取某个cookie数据
    Cookie::get('name');

    2. Cookie 構成ファイル

#config ディレクトリの cookie.php ファイル

return [
    // cookie 保存时间
    'expire'    => 0,
    // cookie 保存路径
    'path'      => '/',
    // cookie 有效域名
    'domain'    => '',
    //  cookie 启用安全传输
    'secure'    => false,
    // httponly设置
    'httponly'  => false,
    // 是否使用 setcookie
    'setcookie' => true,
];
    3. キャッシュ
  • 使用するにはキャッシュを呼び出すには、ファサード メソッド (
think\facade\Cache

) を使用する必要があります。

  • サポートされる組み込みキャッシュ タイプには、file が含まれます。 、

    memcache
  • wincache

    sqlite redis1. キャッシュを使用する

    // 缓存在3600秒之后过期
    Cache::set('number', 10, 3600);
    // number自增(步进值为3)
    Cache::inc('number',3);
    // number自减(步进值为1)
    Cache::dec('number');
    // 获取缓存
    Cache::get('number');
    // 删除缓存
    Cache::delete('number');
    // push 追加缓存
    Cache::set('name', ['欧阳克','朱老师']);
    Cache::push('name', '西门大官人');
    // 获取并删除缓存
    Cache::pull('name');
    // 清空缓存
    Cache::clear();
    2. キャッシュ設定ファイル

#config ディレクトリcache.php ファイル

#
return [
    // 默认缓存驱动
    'default' => 'file',
    // 缓存连接方式配置
    'stores'  => [
        'file' => [
            // 驱动方式
            'type'       => 'File',
            // 缓存保存目录
            'path'       => '',
            // 缓存前缀
            'prefix'     => '',
            // 缓存有效期 0表示永久缓存
            'expire'     => 0,
            // 缓存标签前缀
            'tag_prefix' => 'tag:',
            // 序列化机制 例如 ['serialize', 'unserialize']
            'serialize'  => [],
        ],
        // redis缓存
        'redis'   =>  [
            // 驱动方式
            'type'   => 'redis',
            // 服务器地址
            'host'   => '127.0.0.1',
        ],
        // 更多的缓存连接
    ],
];

4. パブリック コントローラー

  • BaseController.php
  • デフォルトの基本コントローラー クラス

use think\facade\View;
use think\facade\Db;
use think\facade\Session;
public function initialize(){
    $session = Session::all();
    if(empty($session['uid'])){
        echo '<script type="text/javascript">alert("请登录!");window.location.href = "/index.php/login/index"; </script>';
        exit;
    }
    $login = $session['account'];
    # 左侧菜单
    $menu = Db::table('shop_menu')->where('fid',0)->select();
    $left = $menu->toArray();
    foreach($left as &$left_v){
        $left_v['lists'] = Db::table('shop_menu')->where('fid',$left_v['id'])->select();
    }
    View::assign([
        'login' => $login,
        'left' => $left,
    ]);
}
Index/Index.php
namespace app\controller;
use app\BaseController;
use think\facade\View;
use think\facade\Db;
use think\facade\Request;
use app\model\Goods;
class Index extends BaseController{
    public function index(){
        $title = '商城';
        # 右侧列表
        $param = Request::param();
        if(isset($param['status']) && $param['status'] == 1){
            $where['status'] = 1;
        }else if(isset($param['status']) && $param['status'] == 2){
            $where['status'] = 2;
        }else{
            $where = true;
        }
        $p = isset($param['p']) ? $param['p'] : 1;
        $db = new Goods();
        $order = [
            'add_time DESC',
            'id DESC'
        ];
        $right = $db->get_all($where,$order,$p,5);
        View::assign([
            'title'  => $title,
            'right' => $right['data'],
            'count' => $right['count'],
            'p' => $p,
            'status' => isset($param['status']) ? $param['status'] : 0
        ]);
        return View::fetch();
    }
}
    5. ファサード
  • ファサードコンテナ内の (動的) クラスに静的呼び出しインターフェイスを提供します。従来の静的メソッド呼び出しと比較して、テスト容易性と拡張性を向上させるために、非静的クラスに対して

    facade
  • class
を定義できます。図書館#(アニメーション) クラスファサードクラスthink\Appthink\facade\Appthink\Cachethink\facade\Cachethink\Configthink\facade\Configthink\Cookiethink\facade\Cookiethink\Db think\facade\Dbthink\Env think\facade\Envthink \イベントthink\facade\Eventthink\ファイルシステムthink\facade\ファイルシステム #think\Lang think\facade\Langthink\Log think\facade\Logthink\ミドルウェアthink\facade\Middlewarethink\Requestthink\facade\Requestthink\Responsethink\facade\Responsethink\Routethink\facade\Routethink\Sessionthink\facade\Session#

1. ファサード クラス

  • ファサードは、コンテナ内の (動的) クラスの静的呼び出しインターフェイスを提供します。従来の静的メソッド呼び出しと比較して、より優れたテスト容易性とスケーラビリティをもたらします

  • システムはほとんどのコア クラス ライブラリに対して Facade を定義しているため、Facade

use think\facade\App;
use think\facade\Db;
use think\facade\View;
use think\facade\Request;
use think\facade\Session;
class Index{
    public function index(){
        // 数据库操作
        $select = Db::table('shop_goods')->select();
        // 请求对象
        $param = Request::param();
        // Session
        $session = Session::all();
        // 视图
        return View::fetch();
    }
}

2. (動的) クラス ライブラリを通じてこれらのシステム クラスにアクセスできます。

use think\App;
use think\Db;
use think\View;
use think\Request;
use think\Session;
class Index{
    public function index(View $view,Db $db){
        $select = $db->table('shop_goods')->select();
        $view->assign([
            'name' => '欧阳克',
            'select' => $select
        ]);
        return $view->fetch();
    }
}

6. アシスタント機能

  • Thinkphp システムは、いくつかの一般的な操作メソッドのアシスタント機能をカプセル化しています

think\Validatethink\facade\Validate
think\View think\facade\View
#アシスタント関数説明abort 実行を中止して送信HTTP ステータス コードapp コンテナ内のインスタンスの迅速な取得は依存関係の挿入をサポートbind ファストバインディングオブジェクトインスタンスcache キャッシュ管理class_basename クラス名を取得(名前空間は含まれません)class_uses_recursive クラスで使用されているすべての特性を取得しますconfigGetそして構成パラメータを設定しますcookie Cookie管理download Get\think\response\オブジェクト インスタンスのダウンロードダンプ ブラウザ フレンドリーな変数出力env 環境変数の取得event トリガーイベントhalt 変数デバッグ出力と割り込み実行input 入力データの取得はデフォルト値とフィルタリングをサポートします##リダイレクト リダイレクト出力リクエスト Get現在のリクエスト オブジェクトレスポンス レスポンス オブジェクトのインスタンス化セッション セッション管理 トークン フォーム トークン出力の生成トレース ログ情報の記録trait_uses_recursive トレイト内で参照されているすべてのトレイトを取得します#url URL 生成validate インスタンス化されたバリデータview テンプレート出力のレンダリング#root_path アプリケーション ルート ディレクトリruntime_path アプリケーション ランタイム ディレクトリ
invoke リフレクション実行 callable の呼び出しは依存関係の挿入をサポートします
json JSON データ出力
jsonpJSONP データ出力
lang 言語変数値の取得
parse_name 文字列命名スタイルの変換
表示 レンダリング コンテンツ出力
xml XML データ出力
app_path 現在のアプリケーション ディレクトリ
base_path アプリケーション ベース ディレクトリ
config_path アプリケーション構成ディレクトリ
#public_path Web ルート ディレクトリ


# #/ / 入力データを取得します。Request::param()


$param = input();

// 変数のデバッグ出力と実行の中断

$ shop = Db::table('shop_goods')->select();

halt($shop);

// View::fetch() 効果を伴うテンプレート出力のレンダリング 同じ

return view();

7. デバッグ

1. デバッグ モードとトレース デバッグ

    root
  • ディレクトリ内の .env

    ファイル

// デバッグ モードとトレース デバッグをオンにします

APP_DEBUG = true

準備: 正式なデプロイ後にデバッグ モードをオフにする

2. 変数のデバッグ

#ThinPHP には組み込みの
    dump
  • デバッグ メソッド

  • $shop = Db::table('shop_goods')->select();
dump($shop);

#

ホットAIツール
Undress AI Tool
Undress AI Tool

脱衣画像を無料で

AI Clothes Remover
AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undresser.AI Undress
Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

Stock Market GPT
Stock Market GPT

AIを活用した投資調査により賢明な意思決定を実現

人気のツール
メモ帳++7.3.1
メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版
SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6
ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版
SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)