简体   繁体   中英

How to call a function inside yii framework controller from core php file?

I am working in yii framework.I am getting stuck at a point where I have to call a function inside controller in yii framework from core php file. Actually I am going to create html snapshot.

my folder structure is

seoPravin--
--protected
  --modules
    --kp
      --Dnycontentcategoriescontroller.php
      --DnycontentvisitstatController.php
--themes
--start.php (This is my customized file)
--index.php

1) Code of start.php file :--

<!DOCTYPE HTML>
     <html>
     <head>
        <?php 
        if (!empty($_REQUEST['_escaped_fragment_']))    
        { 
            $yii=dirname(__FILE__).'/yii_1.8/framework/yii.php';
            require_once($yii);
            $escapeFragment=$_REQUEST['_escaped_fragment_'];
            $arr=explode('/',$escapeFragment);
            include 'protected/components/Controller.php';
            include 'protected/modules/'.$arr[0].'/controllers/'.$arr[1].'Controller.php';

            echo DnycontentcategoriesController::actiongetDnyContent();  //gettting error at this point

            ?>
            </head>
            <body>
                <?php 
                    //echo "<br> ".$obj->actiongetDnyContent();
        }
                ?>
            </body>
    </html>             

2) yii side controller function : This function work for normal but when I am calling using escaped_fragment it gives error

public static function actiongetDnyContent()
    {

        if (!empty($_REQUEST['_escaped_fragment_']))//code inside if statement not working
        {
            $escapedFragment=$_REQUEST['_escaped_fragment_'];
            $arr=explode('/',$escapedFragment);
            $contentTitleId=end($arr);
            $model = new Dnycontentvisitstat();  //Error got at this line
        }
        else  //Below code is working properly  
        {
            $dependency = new CDbCacheDependency('SELECT MAX(createDateTime) FROM dnycontent');
            $content = new Dnycontent();
            $content->contentTitleId = $_GET['contentTitleId'];
            $content = $content->cache(2592000,$dependency)->getContent();
            $userId=105;
            $ipAddress=Yii::app()->request->userHostAddress;
            echo "{\"contents\":[".CJSON::encode($content)."]} ";
            $model = new Dnycontentvisitstat();
            $model->save($_GET['contentTitleId'], $userId, $ipAddress);
        }   
    }

error:

Fatal error: Class 'Dnycontentvisitstat' not found in C:\\wamp\\www\\seoPravin\\protected\\modules\\KnowledgePortal\\controllers\\DnycontentcategoriesController.php on line 289

code is working for normal url but not working for _esaped_fragment

It is a very bad practice but you can do a HTTP self request, like this:

include("http://{$_SERVER['HTTP_HOST']}/path/?r=controller/action&_param={$_GET['param']}");

Check http://www.php.net/manual/en/function.include.php to see how to enable HTTP includes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM