admin管理员组文章数量:1023263
Is there are any way to check if an arbitrary bination of controller/action exists? (Not the current one.)
Something like Yii::$app->exist(controller/action);
Should it be possible check the route or something so?
What I need is to check if a parameter passed as
<?php echo Yii::$app->request->baseUrl.'/controller/action' ?>
to a JavaScript generic function exists before its execution via Ajax.
Is there are any way to check if an arbitrary bination of controller/action exists? (Not the current one.)
Something like Yii::$app->exist(controller/action);
Should it be possible check the route or something so?
What I need is to check if a parameter passed as
<?php echo Yii::$app->request->baseUrl.'/controller/action' ?>
to a JavaScript generic function exists before its execution via Ajax.
Share Improve this question edited Apr 2, 2015 at 1:57 AstroCB 12.4k20 gold badges59 silver badges74 bronze badges asked Apr 1, 2015 at 23:44 Tino FernándezTino Fernández 10810 bronze badges3 Answers
Reset to default 4You may check this with use method_exists
. Like that:
method_exists(Yii::$app->controllerNamespace . $controllerName, 'action' . ucfirst($actionName));// $actionName with first lette is uppercase
For more - http://php/manual/en/function.method-exists.php
EDIT:
Or you may use this way:
$controller = Yii::$app->createController('controller');//
if (!$controller !== null && method_exists($controller, 'action')) {
echo 'controller/action is allow :)';
}
Or I invented better way with use Yii2 Api:
$controller = Yii::$app->createController('controller');//
if (!$controller !== null && $controller->hasMethod('action'))) {
echo 'controller/action is allow :)';
}
The most reliable way to do this is to create Action instance.
public function actionExists($controllerId, $actionId, $module = null)
{
if ($module === null) {
$module = Yii::$app;
}
$controller = $module->createControllerByID($controllerId);
if ($controller === null) {
return false;
}
$action = $controller->createAction($actionId);
if ($action === null) {
return false;
}
return true;
}
Inspired by @vitalik_74, after referring the source code and testing I found this works in Yii 1.1:
function isActionExistsInController($actionId, $controllerId, $moduleId = null) {
$route = $moduleId ? $moduleId.'/'.$controllerId.'/'.$actionId : $controllerId.'/'.$actionId;
$controller = Yii::app()->createController($route);
return !!$controller;
}
Is there are any way to check if an arbitrary bination of controller/action exists? (Not the current one.)
Something like Yii::$app->exist(controller/action);
Should it be possible check the route or something so?
What I need is to check if a parameter passed as
<?php echo Yii::$app->request->baseUrl.'/controller/action' ?>
to a JavaScript generic function exists before its execution via Ajax.
Is there are any way to check if an arbitrary bination of controller/action exists? (Not the current one.)
Something like Yii::$app->exist(controller/action);
Should it be possible check the route or something so?
What I need is to check if a parameter passed as
<?php echo Yii::$app->request->baseUrl.'/controller/action' ?>
to a JavaScript generic function exists before its execution via Ajax.
Share Improve this question edited Apr 2, 2015 at 1:57 AstroCB 12.4k20 gold badges59 silver badges74 bronze badges asked Apr 1, 2015 at 23:44 Tino FernándezTino Fernández 10810 bronze badges3 Answers
Reset to default 4You may check this with use method_exists
. Like that:
method_exists(Yii::$app->controllerNamespace . $controllerName, 'action' . ucfirst($actionName));// $actionName with first lette is uppercase
For more - http://php/manual/en/function.method-exists.php
EDIT:
Or you may use this way:
$controller = Yii::$app->createController('controller');//
if (!$controller !== null && method_exists($controller, 'action')) {
echo 'controller/action is allow :)';
}
Or I invented better way with use Yii2 Api:
$controller = Yii::$app->createController('controller');//
if (!$controller !== null && $controller->hasMethod('action'))) {
echo 'controller/action is allow :)';
}
The most reliable way to do this is to create Action instance.
public function actionExists($controllerId, $actionId, $module = null)
{
if ($module === null) {
$module = Yii::$app;
}
$controller = $module->createControllerByID($controllerId);
if ($controller === null) {
return false;
}
$action = $controller->createAction($actionId);
if ($action === null) {
return false;
}
return true;
}
Inspired by @vitalik_74, after referring the source code and testing I found this works in Yii 1.1:
function isActionExistsInController($actionId, $controllerId, $moduleId = null) {
$route = $moduleId ? $moduleId.'/'.$controllerId.'/'.$actionId : $controllerId.'/'.$actionId;
$controller = Yii::app()->createController($route);
return !!$controller;
}
本文标签: javascriptCheck if an arbitrary controlleraction existStack Overflow
版权声明:本文标题:javascript - Check if an arbitrary controlleraction exist - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745525820a2154518.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论