日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

yii2 smarty php,YII2 整合smarty

發布時間:2024/8/1 php 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 yii2 smarty php,YII2 整合smarty 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近 在研究YII2這個框架,主要研究了下,依賴注入(DI或者IOC)和事件(AOP),這個框架對于之前做過java編程的人來說無疑是最好的,因為它的風格和Spring結構是一樣的設計,本人以前就是從事java開發的,所以接觸了這個框架之后很是喜歡,用著很順手,比CI框架好了很多,TP雖然也是面向對象的,但是跟YII比起來,代碼方面還是差了點,YII簡潔的代碼,用著很舒服,因為YII放棄了對smarty的整合,使用原生的PHP,提高了編譯效率,但是,畢竟前端和后端是分開的,使用smarty對前端來說是最好的選擇,整合smarty官方建議直接使用composer下載,但是我討厭這樣于是自定義進行了整合:

首先,下載smarty模版百度一下,下載一個吧,這里就不做連接了。

將smarty壓縮包解壓放到一個自己定義的擴展文件夾,我放在了YII默認的 frontend/libs/smarty(自己創建)下面,然后在libs下面定義一個smarty的子類來擴展smarty為

CSmarty.php

namespace frontend\libs;

use Yii;

define("BASEPATH",\Yii::$app->basePath);

require_once \Yii::$app->basePath."/libs/smarty/Smarty.class.php";

define('SMARTY_TMPDIR',BASEPATH.'/views/templates/');//放置模版的目錄 //自己創建

define('SMARTY_CACHEDIR',BASEPATH . '/views/template_cache/');//緩存文件目錄

define('LIFTTIME',1800);

define('SMARTY_DLEFT', '

define('SMARTY_DRIGHT', '}>');//右限定符

class CSmarty extends \Smarty{

protected static $_instance = NULL;

static function getInstance(){

if(self::$_instance == NULL){

self::$_instance = new CSmarty();

}

return self::$_instance;

}

function __construct(){

parent::__construct();

$this->template_dir = SMARTY_TMPDIR;

$this->compile_dir = SMARTY_COMPILE;

$this->config_dir = SMARTY_CONFIG;

$this->compile_check = true;

$this->caching = 1;

$this->cache_dir = SMARTY_CACHEDIR;

$this->left_delimiter = SMARTY_DLEFT;

$this->right_delimiter = SMARTY_DRIGHT;

$this->cache_lifetime = LIFTTIME;

}

function init(){

}

}

配置frontend/config/main.php

$params = array_merge(

require(__DIR__ . '/../../common/config/params.php'),

require(__DIR__ . '/../../common/config/params-local.php'),

require(__DIR__ . '/params.php'),

require(__DIR__ . '/params-local.php')

);

//配置自己需要的組建

return [

'id' => 'app-frontend',

'basePath' => dirname(__DIR__),

'bootstrap' => ['log'],

'controllerNamespace' => 'frontend\controllers',

'components' => [

'user' => [

'identityClass' => 'common\models\User',

'enableAutoLogin' => true,

],

'log' => [

'traceLevel' => YII_DEBUG ? 3 : 0,

'targets' => [

[

'class' => 'yii\log\FileTarget',

'levels' => ['error', 'warning'],

],

],

],

'errorHandler' => [

'errorAction' => 'site/error',

],

'cache'=>[

'class' =>'yii\\caching\\FileCache'

],

'mailer'=>[

'mailer' =>'yii\\mail\\BaserMailer'

],

'urlManager' => [

'class' => 'yii\web\urlManager',

'enablePrettyUrl' => true,

],

'smarty'=>[

'class'=>'frontend\libs\CSmarty'//將自己定義CSmarty的命名空間放在這里

],

],

'params' => $params,

];

最后一步就可以引用smarty了

public function actionTest(){

$smarty = \Yii::$app->smarty;

$smarty->assign('name','HelloSmarty');

$smarty->display("index.html");

}訪問自己的控制器就會跳轉到自己定義的模版了:

版權聲明:本文為博主原創文章,未經博主允許不得轉載。

原文:http://blog.csdn.net/gaoxuaiguoyi/article/details/46967207

總結

以上是生活随笔為你收集整理的yii2 smarty php,YII2 整合smarty的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。