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;
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745525821a4631463.html
评论列表(0条)