YII使用url组件美化管理的方法,yiiurl组件美化
本文实例讲述了YII使用url组件美化管理的方法。分享给大家供大家参考,具体如下:
urlManager组件
yii的官方文档对此的解释如下:
urlSuffix 此规则使用的url后缀,默认使用CurlManger::urlSuffix,值为null。例如可以将此设置为.html,让url看起来“像”是一个静态页面。
caseSensitive 是否大小写敏感,默认使用CUrlManager::caseSensitive,值为null。
defaultParams 该规则使用的默认get参数。当使用该规则来解析一个请求时,这个参数的值会被注入到$_GET参数中。
matchValue 当创建一个URL时,GET参数是否匹配相应的子模式。默认使用CurlManager::matchValue,值为null。
如果该属性为 false,那么意味着当路由和参数名匹配给定的规则时,将以此来创建一个URL。
如果该属性为true,那么给定的参数值夜必须匹配相应的参数子模式。
注意:将此属性设置为true会降低性能。
我们使用一些例子来解释网址工作规则。我们假设我们的规则包括如下三个:
array( 'posts'=>'post/list', 'post/<id:\d+>'=>'post/read', 'post/<year:\d{4}>/<title>'=>'post/read', )
调用$this->createUrl('post/list')生成/index.php/posts。第一个规则适用。
调用$this->createUrl('post/read',array('id'=>100))生成/index.php/post/100。第二个规则适用。
调用$this->createUrl('post/read',array('year'=>2008,'title'=>'a sample post'))生成/index.php/post/2008/a%20sample%20post。第三个规则适用。
调用$this->createUrl('post/read')产生/index.php/post/read。请注意,没有规则适用。
总之,当使用createUrl生成网址,路线和传递给该方法的GET参数被用来决定哪些网址规则适用。如果关联规则中的每个参数可以在GET参数找到的,将被传递给createUrl ,如果路线的规则也匹配路线参数,规则将用来生成网址。
如果GET参数传递到createUrl是以上所要求的一项规则,其他参数将出现在查询字符串。例如,如果我们调用$this->createUrl('post/read',array('id'=>100,'year'=>2008)) ,我们将获得/index.php/post/100?year=2008。为了使这些额外参数出现在路径信息的一部分,我们应该给规则附加/* 。 因此,该规则post/
正如我们提到的,URL规则的其他用途是解析请求网址。当然,这是URL生成的一个逆过程。例如, 当用户请求/index.php/post/100 ,上面例子的第二个规则将适用来解析路线post/read和GET参数array('id'=>100) (可通过$_GET获得) 。
提示:此网址通过createurl方法所产生的是一个相对地址。为了得到一个绝对的url ,我们可以用前缀yii: :app()->hostInfo ,或调用createAbsoluteUrl 。
注:使用的URL规则将降低应用的性能。这是因为当解析请求的URL ,[ CUrlManager ]尝试使用每个规则来匹配它,直到某个规则可以适用。因此,高流量网站应用应尽量减少其使用的URL规则。
test.com/vthot 想生成 test.com/vthot/
复制代码 代码如下:'urlSuffix'=>'/',
要更改URL格式,我们应该配置urlManager应用元件,以便createUrl可以自动切换到新格式和应用程序可以正确理解新的网址:
'urlManager'=>array( 'urlFormat'=>'path', 'showScriptName'=>false, 'urlSuffix'=>'.html', 'rules'=>array( 'posts'=>'post/list', 'post/<id:\d+>'=>array('post/show','urlSuffix'=>'.html'), 'post/<id:\d+>/<mid:\w+>'=>array('post/view','urlSuffix'=>'.xml'), ), ),
示例一
Rule代码
复制代码 代码如下:'posts'=>'post/list',
Action代码
复制代码 代码如下:echo $this->createAbsoluteUrl('post/list');
输出
http://localhost/test/index.php/post
示例二
Rule代码
复制代码 代码如下:'post/
Action代码
复制代码 代码如下:echo $this->createAbsoluteUrl('post/show',array('id'=>998, 'name'=>'123'));
输出
http://localhost/test/index.php/post/998.html?name=123
示例三
Rule代码:
复制代码 代码如下:'post/
Action代码
复制代码 代码如下:echo $this->createAbsoluteUrl('post/view',array('id'=>998, 'mid'=>'tody'));
输出
http://localhost/test/index.php/post/998/tody.xml
示例四
Rule代码
复制代码 代码如下:'http://
Action代码:
echo $this->createAbsoluteUrl('look/host',array('user'=>'boy','mid'=>'ny-01')); echo ''; echo $this->createAbsoluteUrl('looks/host',array('user'=>'boy','mid'=>'ny-01'));
输出
http://boy.vt.com/look.me?mid=ny-01
http://localhost/test/index.php/looks/host/user/boy/mid/ny-01
1)controller/Update/id/23
public function actionUpdate(){ $id = Yii::app()->request->getQuery('id') ; 经过处理的$_GET['id'] } //$id = Yii::app()->request->getPost('id'); 经过处理的$_POST['id'] //$id = Yii::app()->request->getParam('id'); //CHttpRequest更多
2)public function actionUpdate($id) 这种不支持多主键,会检查一下到底GET里面有没有id,没有id就直接不允许访问
'sayhello/<name>' => 'post/hello', name是PostController actionHello($name)的参数 'post/<alias:[-a-z]+>' => 'post/view', domain/post/e文小写 其中:前面的alias是PostController actionView($alias)的参数 '(posts|archive)/<order:(DESC|ASC)>' => 'post/index', domain/posts/DESC或domain/posts/ASC '(posts|archive)' => 'post/index', domain/posts或domain/archive 'tos' => array('website/page', 'defaultParams' => array('alias' =>'terms_of_service')),
When the URL is /tos, pass terms_of_service as the alias parameter value.
隐藏 index.php
还有一点,我们可以做进一步清理我们的网址,即在URL中藏匿index.php 入口脚本。这就要求我们配置Web服务器,以及urlManager应用程序元件。
1.add showScriptName=>false
2.add project/.htaccess
RewriteEngine on # if a directory or a file exists, use it directly RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # otherwise forward it to index.php RewriteRule . index.php
3.开启rewrite
简单的说,在main.php中简单设置urlManager,然后讲了3条规则,基本都覆盖到了。最后是隐藏index.php,请记住.htaccess位于index.php同级目录 ,而不是protected/目录。其他就简单了。
希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。
您可能感兴趣的文章:
- YiiFramework入门知识点总结(图文教程)
- Yii入门教程之目录结构、入口文件及路由设置
- Yii入门教程之Yii安装及hello world
- Yii PHP Framework实用入门教程(详细介绍)
- Yii查询生成器(Query Builder)用法实例教程
- Yii实现单用户博客系统文章详情页插入评论表单的方法
- Yii中CGridView实现批量删除的方法
- Yii快速入门经典教程

phpisusedforsendendemailsduetoitsignegrationwithservermailservicesand andexternalsmtpproviders,自動化intifications andMarketingCampaigns.1)設置設置yourphpenvenvironnvironnvironmentwithaweberswithawebserverserververandphp,確保themailfunctionisenabled.2)useabasicscruct

發送電子郵件的最佳方法是使用PHPMailer庫。 1)使用mail()函數簡單但不可靠,可能導致郵件進入垃圾郵件或無法送達。 2)PHPMailer提供更好的控制和可靠性,支持HTML郵件、附件和SMTP認證。 3)確保正確配置SMTP設置並使用加密(如STARTTLS或SSL/TLS)以增強安全性。 4)對於大量郵件,考慮使用郵件隊列系統來優化性能。

CustomHeadersheadersandAdvancedFeaturesInphpeMailenHanceFunctionalityAndreliability.1)CustomHeadersheadersheadersaddmetadatatatatataatafortrackingandCategorization.2)htmlemailsallowformattingandttinganditive.3)attachmentscanmentscanmentscanbesmentscanbestmentscanbesentscanbesentingslibrarieslibrarieslibrariesliblarikelikephpmailer.4)smtppapapairatienticationaltication enterticationallimpr

使用PHP和SMTP發送郵件可以通過PHPMailer庫實現。 1)安裝並配置PHPMailer,2)設置SMTP服務器細節,3)定義郵件內容,4)發送郵件並處理錯誤。使用此方法可以確保郵件的可靠性和安全性。

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

使用依賴注入(DI)的原因是它促進了代碼的松耦合、可測試性和可維護性。 1)使用構造函數注入依賴,2)避免使用服務定位器,3)利用依賴注入容器管理依賴,4)通過注入依賴提高測試性,5)避免過度注入依賴,6)考慮DI對性能的影響。

phpperformancetuningiscialbecapeitenhancesspeedandeffice,whatevitalforwebapplications.1)cachingwithapcureduccureducesdatabaseloadprovesrovessetimes.2)優化

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。