Tuesday, November 6, 2012

strange behavior with redirecting inside the Controller


Hi bakers,

first of all, my english is not good....

i have a strange behavior with redirecting inside the Controller. Well, i must redirect(see below/end of public function index()) to the self controller(ModsitemapController) to bypassing the php _max_execute_time. If cake begin to redirect i get a redirection error. But if i redirect to other controller the redirect work. Whats i do wrong? is that a bug?

<?php

//Configure::write('debug', 2);
class ModsitemapController extends AppController {
   
    public $theme  = 'Frontend';
    public $layout = 'modsitemap';
   
    public $lastSpidertCatId = 0;
    public $catDetailSiteReached = 0;
    public $catDetailSiteTotal = 0;
    public $cachedCatId = 0;
   
    public function __construct($request = null, $response = null) {
       
        parent::__construct($request, $response);
       
        if(isset($request->params['pass'][0])){
            $this->lastSpidertCatId = (int)$request->params['pass'][0];
        }
        if(isset($request->params['pass'][1])){
            $this->catDetailSiteReached = (int)$request->params['pass'][1];
        }
        if(isset($request->params['pass'][2])){
            $this->catDetailSiteTotal = (int)$request->params['pass'][2];
        }
    }
   
    public function index(){
       
        $this->loadModel('Category');
       
        $res = $this->Category->find('all', array(
            'conditions' => array(
                'Category.blacklist' => 0,
                'Category.is_searched_by_user' => 1,
                'Category.id >=' => $this->lastSpidertCatId
            ),
            'fields' => array('Category.id', 'Category.name'),
            'recursive' => -1
        )); $x = 1;
       
        foreach($res as $key => $cat){

            $this->cachedCatId = $cat['Category']['id'];
           
            $urls = $this->Category->Url->find('all', array(
                'conditions' => array(
                    'Url.category_id' => $cat['Category']['id']
                ),
                'fields' => array('Url.name, Url.id'),
                'recursive' => -1
            ));

            if(isset($urls[0]['Url']['id'])){

                    foreach($urls as $url){

                        $content = $this->Category->Content->find('first', array(
                            'conditions' => array(
                                'Content.category_id' => $cat['Category']['id'],
                                'Content.url_id' => $url['Url']['id'],
                            ),
                            'recursive' => -1,
                            'fields' => array('Content.id')
                        ));

                        if(isset($content['Content']['id'])){

                            $hostForUrl = str_replace('www.', '', parse_url($url['Url']['name'], PHP_URL_HOST));
                            $explodedHostUrl = explode('.', $hostForUrl);
                            $popElement = array_pop($explodedHostUrl);
                            $implodtedHostUrl = implode('.', $explodedHostUrl);
                            $aHref = "/$implodtedHostUrl/$popElement/";

                            $keyword = str_replace(' ', '-', $cat['Category']['name']);  $x++;
                           

                            $this->xmlString .=
                                "<url>
                                    <loc>http://domain.net/{$keyword}{$aHref}</loc>
                                    <lastmod>".$this->getDate()."</lastmod>
                                    <priority>0.60</priority>
                                </url>";

                            if($x === 50000){

                                $this->buildXmlString();
                                $this->saveXmlFile();
                                $this->resetXmlString();
                                break;

                            }
                        }
                    }
            }
           
            if($x === 50000){ break; }
           
        }

        $this->redirect(array('controller' => 'modsitemap', 'action' => 'index', (int)$this->cachedCatId));
       
    }
   
    protected function getDate(){ /***********/ }
}

?>

No comments: