yii2.0框架基本功能課件_第1頁
yii2.0框架基本功能課件_第2頁
yii2.0框架基本功能課件_第3頁
yii2.0框架基本功能課件_第4頁
yii2.0框架基本功能課件_第5頁
已閱讀5頁,還剩17頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、linux服務器安裝phalcon框架Phalcon是一個用C寫的PHP框架在兼具功能的同時,性能強于市面上一票的熱門框架下面咱們就來安裝它假設你是CentOS并且通過源方式安裝的php那么你需要執(zhí)行下面的命令yum install git gcc libtoolgit clone -depth=1 git:/cd cphalcon/buildsudo ./install安裝完畢之后在/etc/php.d/下創(chuàng)建50-phalcon.ini并且寫入如下內容extension = phalcon.so然后重啟php-fpm服務搞定安裝.windows安裝phalcon大家在解壓縮下載的Phalc

2、on框架包之后,會發(fā)現(xiàn)一個php_phalcon.dll文件,沒錯,就是它,你就可以學習國外先進的框架。那么我們如何使用它呢?把php_phalcon.dll文件copy到php目錄里面的ext目錄里面,然后打開php.ini文件在最底部加入一行代碼:extension=php_phalcon.dll開啟Apache的rewrite_module模塊,重啟Apache,這時你就可以使用Phalcon框架了。創(chuàng)建簡單的文件目錄結構:test/ |-app/ |-controllers /控制器 |-models /模型 |-views /視圖 |-public/ |-css/ |-img/ |-

3、js/ |-.htaccess |-index.php |-.htaccess項目目錄test下的.htaccess文件內容如下:#/test/.htaccess<IfModule mod_rewrite.c> RewriteEngine on RewriteRule $ public/ L RewriteRule (.*) public/$1 L</IfModule>Public目錄下的.htaccess文件內容如下:#/test/public/.htaccess<IfModule mod_rewrite.c> RewriteEngine On Rewri

4、teCond %REQUEST_FILENAME !-d RewriteCond %REQUEST_FILENAME !-f RewriteRule (.*)$ index.php?_url=/$1 QSA,L</IfModule>下面就是引導程序了:<?phptry /這部分是自動加載 $loader = new PhalconLoader(); $loader->registerDirs(array( './app/controllers/', './app/models/' )->register(); /依賴注入 $di =

5、 new PhalconDIFactoryDefault(); /設置視圖組件 $di->set('view', function() $view = new PhalconMvcView(); $view->setViewsDir('./app/views/'); return $view; ); /設置URL $di->set('url', function() $url = new PhalconMvcUrl(); $url->setBaseUri('/test/'); return $url; );

6、 $application = new PhalconMvcApplication($di); echo $application->handle()->getContent(); catch(PhalconException $e) echo "PhalconException: ", $e->getMessage();創(chuàng)建控制器:在app/controllers文件下創(chuàng)建一個IndexController.php文件:<?phpclass IndexController extends PhalconMvcController public fu

7、nction indexAction() echo "<h1>Hello!</h1>" 完成這些之后,在瀏覽器里輸入http:/localhost/test/是不是就會出現(xiàn)“Hello!”了?什么?沒有?肯定是你代碼寫錯了,好好檢查。下面是總結的幾個使用方法,include PHPExcel.php;include PHPExcel/Writer/Excel2007.php;/或者include PHPExcel/Writer/Excel5.php; 用于輸出.xls的創(chuàng)建一個excel$objPHPExcel = new PHPExcel();保存

8、excel2007格式$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);/或者$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); 非2007格式$objWriter->save(“xxx.xlsx”);直接輸出到瀏覽器$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);header(“Pragma: public”);header(“Expires: 0);header(“Cache-Control:must-r

9、evalidate, post-check=0, pre-check=0);header(“Content-Type:application/force-download”);header(“Content-Type:application/vnd.ms-execl”);header(“Content-Type:application/octet-stream”);header(“Content-Type:application/download”);header(Content-Disposition:attachment;filename=”resume.xls”);header(“Con

10、tent-Transfer-Encoding:binary”);$objWriter->save(php:/output);phpExcel中文手冊設置excel的屬性:創(chuàng)建人$objPHPExcel->getProperties()->setCreator(“Maarten Balliauw”);最后修改人$objPHPExcel->getProperties()->setLastModifiedBy(“Maarten Balliauw”);標題$objPHPExcel->getProperties()->setTitle(“Office 2007

11、XLSX Test Document”);題目$objPHPExcel->getProperties()->setSubject(“Office 2007 XLSX Test Document”);描述$objPHPExcel->getProperties()->setDescription(“Test document for Office 2007 XLSX, generated using PHP classes.”);關鍵字$objPHPExcel->getProperties()->setKeywords(“office 2007 openxml

12、php”);種類$objPHPExcel->getProperties()->setCategory(“Test result file”);設置當前的sheet$objPHPExcel->setActiveSheetIndex(0);設置sheet的name$objPHPExcel->getActiveSheet()->setTitle(Simple);設置單元格的值$objPHPExcel->getActiveSheet()->setCellValue(A1, String);$objPHPExcel->getActiveSheet()-&g

13、t;setCellValue(A2, 12);$objPHPExcel->getActiveSheet()->setCellValue(A3, true);$objPHPExcel->getActiveSheet()->setCellValue(C5, =SUM(C2:C4);$objPHPExcel->getActiveSheet()->setCellValue(B8, =MIN(B2:C5);合并單元格$objPHPExcel->getActiveSheet()->mergeCells(A18:E22);分離單元格$objPHPExcel-&

14、gt;getActiveSheet()->unmergeCells(A28:B28);保護cell$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true); / Needs to be set to true in order to enable any worksheet protection!$objPHPExcel->getActiveSheet()->protectCells(A3:E13, PHPExcel);設置格式/ Set cell number formatsecho d

15、ate(H:i:s) . ” Set cell number formatsn”;$objPHPExcel->getActiveSheet()->getStyle(E4)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat:FORMAT_CURRENCY_EUR_SIMPLE);$objPHPExcel->getActiveSheet()->duplicateStyle( $objPHPExcel->getActiveSheet()->getStyle(E4), E5:E13

16、 );設置寬width/ Set column widths$objPHPExcel->getActiveSheet()->getColumnDimension(B)->setAutoSize(true);$objPHPExcel->getActiveSheet()->getColumnDimension(D)->setWidth(12);設置font$objPHPExcel->getActiveSheet()->getStyle(B1)->getFont()->setName(Candara);$objPHPExcel->ge

17、tActiveSheet()->getStyle(B1)->getFont()->setSize(20);$objPHPExcel->getActiveSheet()->getStyle(B1)->getFont()->setBold(true);$objPHPExcel->getActiveSheet()->getStyle(B1)->getFont()->setUnderline(PHPExcel_Style_Font:UNDERLINE_SINGLE);$objPHPExcel->getActiveSheet()-&

18、gt;getStyle(B1)->getFont()->getColor()->setARGB(PHPExcel_Style_Color:COLOR_WHITE);$objPHPExcel->getActiveSheet()->getStyle(E1)->getFont()->getColor()->setARGB(PHPExcel_Style_Color:COLOR_WHITE);$objPHPExcel->getActiveSheet()->getStyle(D13)->getFont()->setBold(true)

19、;$objPHPExcel->getActiveSheet()->getStyle(E13)->getFont()->setBold(true);設置align$objPHPExcel->getActiveSheet()->getStyle(D11)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment:HORIZONTAL_RIGHT);$objPHPExcel->getActiveSheet()->getStyle(D12)->getAlignment()->s

20、etHorizontal(PHPExcel_Style_Alignment:HORIZONTAL_RIGHT);$objPHPExcel->getActiveSheet()->getStyle(D13)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment:HORIZONTAL_RIGHT);$objPHPExcel->getActiveSheet()->getStyle(A18)->getAlignment()->setHorizontal(PHPExcel_Style_Alignmen

21、t:HORIZONTAL_JUSTIFY);/垂直居中$objPHPExcel->getActiveSheet()->getStyle(A18)->getAlignment()->setVertical(PHPExcel_Style_Alignment:VERTICAL_CENTER);設置column的border$objPHPExcel->getActiveSheet()->getStyle(A4)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border:BORDER_

22、THIN);$objPHPExcel->getActiveSheet()->getStyle(B4)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border:BORDER_THIN);$objPHPExcel->getActiveSheet()->getStyle(C4)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border:BORDER_THIN);$objPHPExcel->getActi

23、veSheet()->getStyle(D4)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border:BORDER_THIN);$objPHPExcel->getActiveSheet()->getStyle(E4)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border:BORDER_THIN);設置border的color$objPHPExcel->getActiveSheet()->ge

24、tStyle(D13)->getBorders()->getLeft()->getColor()->setARGB(FF993300);$objPHPExcel->getActiveSheet()->getStyle(D13)->getBorders()->getTop()->getColor()->setARGB(FF993300);$objPHPExcel->getActiveSheet()->getStyle(D13)->getBorders()->getBottom()->getColor()-&

25、gt;setARGB(FF993300);$objPHPExcel->getActiveSheet()->getStyle(E13)->getBorders()->getTop()->getColor()->setARGB(FF993300);$objPHPExcel->getActiveSheet()->getStyle(E13)->getBorders()->getBottom()->getColor()->setARGB(FF993300);$objPHPExcel->getActiveSheet()->

26、getStyle(E13)->getBorders()->getRight()->getColor()->setARGB(FF993300);設置填充顏色$objPHPExcel->getActiveSheet()->getStyle(A1)->getFill()->setFillType(PHPExcel_Style_Fill:FILL_SOLID);$objPHPExcel->getActiveSheet()->getStyle(A1)->getFill()->getStartColor()->setARGB(F

27、F808080);$objPHPExcel->getActiveSheet()->getStyle(B1)->getFill()->setFillType(PHPExcel_Style_Fill:FILL_SOLID);$objPHPExcel->getActiveSheet()->getStyle(B1)->getFill()->getStartColor()->setARGB(FF808080);加圖片$objDrawing = new PHPExcel_Worksheet_Drawing();$objDrawing->setNa

28、me(Logo);$objDrawing->setDescription(Logo);$objDrawing->setPath(./images/officelogo.jpg);$objDrawing->setHeight(36);$objDrawing->setWorksheet($objPHPExcel->getActiveSheet();$objDrawing = new PHPExcel_Worksheet_Drawing();$objDrawing->setName(Paid);$objDrawing->setDescription(Paid

29、);$objDrawing->setPath(./images/paid.png);$objDrawing->setCoordinates(B15);$objDrawing->setOffsetX(110);$objDrawing->setRotation(25);$objDrawing->getShadow()->setVisible(true);$objDrawing->getShadow()->setDirection(45);$objDrawing->setWorksheet($objPHPExcel->getActiveSh

30、eet();/處理中文輸出問題需要將字符串轉化為UTF-8編碼,才能正常輸出,否則中文字符將輸出為空白,如下處理:$str = iconv(gb2312, utf-8, $str);或者你可以寫一個函數(shù)專門處理中文字符串:function convertUTF8($str)if(empty($str) return ”;return iconv(gb2312, utf-8, $str);/從數(shù)據(jù)庫輸出數(shù)據(jù)處理方式從數(shù)據(jù)庫讀取數(shù)據(jù)如:$db = new Mysql($dbconfig);$sql = “SELECT * FROM 表名”;$row = $db->GetAll($sql);

31、/ $row 為二維數(shù)組$count = count($row);for ($i = 2; $i <= $count+1; $i+) $objPHPExcel->getActiveSheet()->setCellValue(A . $i, convertUTF8($row$i-21);$objPHPExcel->getActiveSheet()->setCellValue(B . $i, convertUTF8($row$i-22);$objPHPExcel->getActiveSheet()->setCellValue(C . $i, convert

32、UTF8($row$i-23);$objPHPExcel->getActiveSheet()->setCellValue(D . $i, convertUTF8($row$i-24);$objPHPExcel->getActiveSheet()->setCellValue(E . $i, convertUTF8(date(“Y-m-d”, $row$i-25);$objPHPExcel->getActiveSheet()->setCellValue(F . $i, convertUTF8($row$i-26);$objPHPExcel->getActi

33、veSheet()->setCellValue(G . $i, convertUTF8($row$i-27);$objPHPExcel->getActiveSheet()->setCellValue(H . $i, convertUTF8($row$i-28);在默認sheet后,創(chuàng)建一個worksheetecho date(H:i:s) . ” Create new Worksheet objectn”;$objPHPExcel->createSheet();$objWriter = PHPExcel_IOFactory:createWriter($objExcel,

34、 Excel5);$objWriter-save(php:/output);ThinkPHP整合Uploadify插件實現(xiàn)異步上傳圖片1、首先到官網下載Uploadify插件。2、在頁面引入uploadify.css、jquery.uploadify.min.js。當然你得先把jquery引入。3、配置uploadify:<script type="text/javascript"> $(function () var sid = ':session_id()' $('#file_upload').uploadify( formD

35、ata : 'session_id' : sid, swf : '_PUBLIC_/uploadify/uploadify.swf', uploader : ":U(MODULE_NAME . '/' . CONTROLLER_NAME . '/uploadify')", buttonText : '教師圖片上傳', fileTypeExt : '*.png;*.jpeg;*.jpg;*.gif;', fileTypeDesc : '選擇圖片', onUpload

36、Success : function(file, data, response) var src = '_ROOT_/Uploads/' + data; $('#img').attr('src', src); $('#pic').val(src); ); );</script>上傳處理的地址就是uploadify方法,由于JS對上傳文件的限制,上傳文件不能做到異步上傳。所以這里我把返回圖片的地址賦值給一個隱藏的表單然后在用異步提交到數(shù)據(jù)庫,有更好的方法的童鞋請在下方留言。4、處理上傳圖片的方法代碼: 代碼如下/圖像上傳

37、處理public function uploadify() if (!empty($_FILES) /圖片上傳設置 $config = array( 'maxSize' => 3145728, 'savePath' => '', 'saveName' => array('uniqid',''), 'exts' => array('jpg', 'gif', 'png', 'jpeg'), '

38、autoSub' => true, 'subName' => array('date','Ymd'), ); $upload = new ThinkUpload($config);/ 實例化上傳類 $info = $upload->upload(); if(!$info) /上傳錯誤提示信息 $this->error($upload->getError(); else foreach($info as $file) echo $file'savepath'.$file'savename

39、' PS:在根目錄創(chuàng)建Uploads目錄<img id="img" src="_PUBLIC_/Images/nopic.png" width="480" height="249" border="0" /><br><span>上傳圖片尺寸建議為480*249</span><br><input type="hidden" name="pic" id="pic"&g

40、t;<input id="file_upload" type="file" multiple="true" size="60" />對于出現(xiàn)302 error 的同學,是因為需要傳遞session值給uploadify。在配置uploadify的時候加上這一項就能解決了formData : 'session_id' : sid,當然得先定義var sid = ':session_id()'。希望對大家有所幫助!【Drupal 8】中調試Twig模板中的變量教程常言:工欲善

41、其事,必先利其器。Drupal 主題開發(fā)中技巧也不少,學會這些技巧會起到事半功倍的效果。今天給大家推介的是如何調試打印出Twig模板中的變量,不妨一看!Oops,Drupal 8!在drupal 8 twig模板中,你可以在注釋中找到大部分、常用的模板變量。但是,當一些模塊或者主題引入新變量的時候,該怎么辦呢?Twig模板提供了一個函數(shù)dump,方便你去發(fā)現(xiàn)、檢查這些變量。使用dump函數(shù)有個小前提,你需要啟用twig的debug模式。關于如何啟用twig的debug模式 /node/1903374。檢查單個變量 dump(title) 簡單吧!那我想

42、看所有的變量,怎么整?找出所有變量 dump() 找出變量可用的key dump(_context|keys) Twig中的全局可用變量There are additional global variables available in all Twig templates:_self references the current template and contains advanced information about a template, i.e. the compiled template class name and information about the Twig env

43、ironment._context references the current context and contains all variables passed to the template such as variables sent from theme(), prepared by preprocess, or set in the template. Adding dump() without specifying a variable is equivalent to dump(_context) ._charset references the current charset

44、.友情提示如果你想查看所有的變量,但是dump函數(shù)因為遞歸等原因把內存耗盡了,你可以通過以下方式來處理:<ol> % for key, value in _context % <li> key </li> % endfor %</ol>然后用一個約束條件去檢查這些變量,如 if loop.index = 2 % 這樣就可以找到你所需要的內容啦!Yii框架實現(xiàn)數(shù)據(jù)分頁及排序的例子在控制器:class PostController extends Controller function actionIndex() $criteria = new CD

45、bCriteria(); $count=Post:model()->count($criteria); $pages=new CPagination($count); / elements per page $pages->pageSize=5; $pages->applyLimit($criteria); / sorting $sort = new CSort('Post'); $sort->attributes = array('id', 'title',); $sort->applyOrder($criteri

46、a); $models = Post:model()->findAll($criteria); $this->render('index', array( 'models' => $models, 'pages' => $pages, 'sort' => $sort, ); 在視圖文件中:<p><?php echo $sort->link('id')?></p><p><?php echo $sort->link('

47、;title')?></p><ol><?php foreach($models as $model):?> <li> <h2><?php echo $model->id?> - <?php echo $model->title?></h2> </li><?php endforeach?></ol> <?php $this->widget('CLinkPager', array( 'pages'

48、=> $pages,)?>一. DAO實現(xiàn)分頁.Controller層public function actionReport() $sql = "select remitdate, sum(rate) sumrate from td_delivery group by remitdate order by remitdate desc" $criteria=new CDbCriteria(); $result = Yii:app()->db->createCommand($sql)->query(); $pages=new CPaginati

49、on($result->rowCount); $pages->pageSize=2; $pages->applyLimit($criteria); $result=Yii:app()->db->createCommand($sql." LIMIT :offset,:limit"); $result->bindValue(':offset', $pages->currentPage*$pages->pageSize); $result->bindValue(':limit', $pages-

50、>pageSize); $posts=$result->query(); $this->render('report',array( 'posts'=>$posts, 'pages'=>$pages, ); View層<?php foreach($posts as $row):?> <?php echo CHtml:link($row"remitdate",array('delivery/view','remitdate'=>$row&quo

51、t;sumrate");?> <?php echo $row"sumrate"."<br />" ?> <?php endforeach;?> <?php /分頁widget代碼: $this->widget('CLinkPager',array('pages'=>$pages); ?>優(yōu)點: DAO效率高; 缺點: view層需要自己寫一些樣式,稍顯麻煩一點二. widget實現(xiàn)分頁model層/* * var string attribute

52、: 日運費 (統(tǒng)計用) * 需要對新增加的字段做個聲明 */ public $dayrate; /* * 統(tǒng)計功能: 統(tǒng)計每日的運費 */ public function statistics() $criteria = new CDbCriteria; $criteria->select = 'remitdate, sum(rate) AS dayrate' $criteria->group = 'remitdate' return new CActiveDataProvider(get_class($this), array( 'crit

53、eria'=>$criteria, 'sort'=>array( / 表頭設置點擊排序的字段 'attributes'=>array( 'remitdate', 'dayrate'=>array( 'asc'=>'dayrate', 'desc'=>'dayrate DESC', ) ), 'defaultOrder'=>'remitdate desc', ), ); Controlle

54、r層/* * 運單統(tǒng)計功能: * 按日期統(tǒng)計 */ public function actionReport() $model=new Delivery('statistics'); $model->unsetAttributes(); / clear any default values $this->render('report',array( 'model'=>$model, ); View層<?php $this->widget('zii.widgets.grid.CGridView', ar

55、ray( 'id'=>'delivery-grid', 'dataProvider'=>$model->statistics(), 'filter'=>$model, 'columns'=>array( 'remitdate', 'dayrate', array( 'class'=>'CButtonColumn', ), ),); ?>優(yōu)點: 可以使用自帶的樣式; 缺點: 效率略低.yii數(shù)據(jù)增加 刪除 修改

56、查找的例子1. 查詢數(shù)據(jù)(select)$con = Yii:app()->db;/查詢一行數(shù)據(jù)$noteInfo = $con->createCommand("select * from note where note_id='$nid'")->queryRow();/查詢多行數(shù)據(jù)$noteInfo = $con->createCommand("select * from note where note_id='$nid'")->queryAll();查詢數(shù)據(jù)集合1、$admin=Admin

57、:model()->findAll ($condition,$params);該方法是根據(jù)一個條件查詢一個集合,如: findAll("username=:name",array(":name"=>$username); 2、$admin=Admin:model()->findAllByPk($postIDs,$condition,$params);findAllByPk($id,"name like ':name' and age=:age",array(':name'=>$name,'age'=>$age); 該方法是根據(jù)主鍵查詢一個集合,可以使用多個主鍵,如: findAllByPk(array(1,2); 3、$admin=Admin:model()->findAllByAttributes($attributes,$condition,$params);該方法是根據(jù)條件查詢一個

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論