svyatov/yii-shortcut

Yii框架的快捷方式

v2.0.0 2014-08-25 18:59 UTC

This package is not auto-updated.

Last update: 2024-09-23 15:44:34 UTC


README

Yii框架v1.1的快捷方式 Yii框架

安装

通过Composer(推荐)

php composer.phar require svyatov/yii-shortcut '~2.0'

通过下载

获取最新的发行版并将Y.php文件放入您的应用程序protected/components文件夹。

用法

在小部件中通过路由创建URL

// Standart code
Yii::app()->controller->createUrl('user/login');

// Y code
Y::url('user/login');

获取/设置一些缓存值

// Standart code
Yii::app()->cache->get('user_settings');
Yii::app()->cache->set('user_settings', $userSettings);

// Y code
Y::getCache('user_settings');
Y::setCache('user_settings', $userSettings);

与cookie相同

// Y code
Y::getCookie('user_settings');
Y::setCookie('user_settings', $userSettings);

获取CSRF令牌的值

// Standart code
Yii::app()->request->csrfToken;

// Y code
Y::csrf();

在某个jQuery请求中插入CSRF名称和令牌

// Standart code
<script>
$.post('/user/login', {<?=Yii::app()->request->csrfTokenName;?>: '<?=Yii::app()->request->csrfToken;?>', ...} ...);
</script>

// Y code
<script>
$.post('/user/login', {<?=Y::csrfJsParam();?>, ...} ...);
</script>

带有代码高亮的快速变量转储

// Standart code
echo '<pre>';
CVarDumper::dump($testVariable, 10, true);
Yii::app()->end();

// Y code
Y::dump($testVariable);

不进行模板渲染的快速操作结束(例如,用于AJAX请求)

// Standart code
echo $result;
Yii::app()->end();
// or
echo json_encode($result);
Yii::app()->end();

// Y code
Y::end($result);
// or
Y::endJson($result);

重定向

// Standart code
$this->redirect($this->createUrl('user/settings')); // the shortest example
Yii::app()->request->redirect(Yii::app()->controller->createUrl('user/settings')); // if we inside some widget

// Y code
Y::redirect('user/settings'); // you can use wherever you want, controller/widget, it doesn't matter

检测当前用户状态(他是不是访客或已认证)

// Standart code
if (Yii::app()->user->isGuest) {} // is user a guest?
// or
if (!Yii::app()->user->isGuest) {} // is user authenticated?

// Y code
if (Y::isGuest()) {} // is user a guest?
// or
if (Y::isAuthenticated()) {} // is user authenticated?
// the code speaks for himself, it's more expressive and readable

如您所见,代码量至少减少了2倍。因此,您需要少输入至少2倍,并且您可以至少快2倍地阅读和理解它。

变更日志

  • v2.0.0 / 26.08.2014

    new 添加了新方法:module()和component()。

    chg 移除了受保护的函数_getComponent()。

    chg 移除了函数getRequest()。

    chg 受保护的函数_getValueByComplexKeyFromArray()重命名为getValueByComplexKeyFromArray()并更改为公共的。

    chg 函数getGet()、getPost()和getFiles()分别重命名为GET()、POST()和FILES()。

    chg 函数isAuthed()重命名为isAuthenticated()。

    chg 函数redir()重命名为redirect()。

    chg 函数flashRedir()重命名为flashAndRedirect()。

    chg 函数redirAuthed()和redirGuest()分别重命名为redirectIfAuthenticated()和redirectIfGuest()。

    chg 函数hasAccess()重命名为checkAccess()。

    chg 缓存、cookie和会话的设置器、获取器和删除器重命名以符合常见模式,例如:cacheSet() -> setCache()。

  • v1.3.2 / 05.05.2014

    fix 修复了Composer自动加载。

    chg _getComponent()和_getValueByComplexKeyFromArray()函数的作用域更改为受保护的。

    chg README.md增强和样式更新。

    chg phpDoc注释轻量级重格式化。

  • v1.3.1 / 28.09.2013

    fix 修复了指向仓库的url。没有代码更改。

  • v1.3.0 / 12.07.2013

    new 所有内容均已翻译成英文。

    new 添加了LICENSE文件。

    chg 更新了composer.json文件。

    chg 向cookieDelete()方法添加了第二个参数$options。

    chg 向cookieSet()方法添加了$secure和$httpOnly参数。此外,$value参数现在可以是CHttpCookie类的实例。

    chg csrfJsParam()方法中CSRF令牌名称现在用引号括起来。

    chg flash()方法中默认$message参数值更改为'false',因此现在可以通过传递'null'作为$message参数来删除闪存消息。

    chg 向hasAccess()方法添加了$params和$allowCaching参数。这不会更改以前的行为,但扩展了方法的功能。

    chg 代码重构。

  • v1.2.1 / 25.01.2012

    new 添加了方法getFile()。

  • v1.2.0 / 20.10.2011

    new 添加了方法:format、script、session、sessionGet、sessionSet、sessionDelete。

    new 添加了应用程序组件的内部缓存。

    chg cookieDelete()方法现在返回被删除的cookie对象(如原始方法)。

  • 版本1.1.5 / 29.09.2011

    新增 添加了 dbCmd() 方法。

    修复 修复了 @since phpDoc 标签中的错误。

  • 版本1.1.4 / 27.09.2011

    修复 修复了 cookieSet() 方法中的错误,该错误防止在没有 $expire 参数的情况下设置cookie。

    变更 Cookie 方法现在使用 CCookieCollection 类的本地方法。

  • 版本1.1.3 / 19.07.2011

    新增 添加了方法:getGet、getPost、getRequest、getPdo、hasFlash。

    变更 cache()、cacheDelete()、cacheGet()、cacheSet() 方法现在具有 $cacheId 参数,可以用于选择不同的缓存后端。

    变更 为 endJson() 方法添加了 $options 参数,可以用于传递选项到本地的 json_encode() 函数。

    变更 移除了更多“魔法”。代码重构。

  • 版本1.1.0 / 29.05.2011

    新增 添加了方法:isAjaxRequest、isPutRequest、isDeleteRequest、isPostRequest、isSecureConnection。

    新增 将变更日志添加到 README。将 README.markdown 重命名为 README.md

    变更 移除了方法内部的几乎所有“魔法”。

    变更 为 baseUrl() 方法添加了 $absolute 参数,可以用于获取绝对URL而不是相对URL。

    变更 为 cookieGet() 方法添加了 $default 参数,可以用于在没有指定名称的cookie存在时返回 $default 变量的值。

    变更 为 param() 方法添加了 $default 参数,可以用于在没有指定名称的参数存在时返回 $default 变量的值。同时,方法代码进行了重构。

    变更 修复并增强了 phpDoc 注释。

  • 版本1.0.4 / 05.01.2011

    变更 代码重构。类已上传到GitHub。

  • 版本1.0.3 / 16.12.2010

    修复 修复了 param() 方法中的错误。

  • 版本1.0.2 / 16.12.2010

    新增 添加了 hasAccess() 方法。

    新增 增强 param() 方法。现在键可以包含点分隔的字符串以访问嵌套变量。例如:'site.title' 返回 param['site']['title'] 的值。感谢 sergebezborodov 的想法。

    chg 代码重构。