Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to redirect after view rendering is complete in cakephp 2.3
    text
    copied!<p>I am using cakephp 2.3 and required to redirect user after the excel sheet is downloaded successfully. I am using the cake $this->response->type for setting the view as excel sheet generator.</p> <pre><code>public function admin_export_excel($task_lists = array()) { $task_ids = $task_lists; $global_task_array = array(); $this-&gt;loadModel('Task'); //-&gt; For each task-id run the loop for fetching the related data to generate the report. foreach ($task_ids as $index =&gt; $task_id) { //-&gt; Check if the task exists with the specified id. $this-&gt;Task-&gt;id = $task_id; if (!$this-&gt;Task-&gt;exists()) throw new NotFoundException('Task not found.'); //-&gt; Now check if the logged user is the owner of the specified task. $task_count = $this-&gt;Task-&gt;find('count', array('conditions' =&gt; array('Task.id' =&gt; $task_id, 'Task.user_id' =&gt; $this-&gt;Auth-&gt;user('id')))); if ($task_count == 0) throw new NotFoundException('Task not accessable.'); $task_data = $this-&gt;Task-&gt;find('first', array( 'conditions' =&gt; array( 'Task.id' =&gt; $task_id ), 'contain' =&gt; array( 'TaskForm' =&gt; array( 'fields' =&gt; array('TaskForm.id', 'TaskForm.reference_table') ), 'Project' =&gt; array( 'fields' =&gt; array('Project.id', 'Project.project_name') ), 'User' =&gt; array( 'fields' =&gt; array('User.id', 'User.company_name') ), 'Timezone' =&gt; array( 'fields' =&gt; array('Timezone.id', 'Timezone.name') ) ) ) ); // debug($task_data); $global_task_array[$index] = $task_data; //-&gt; End of Custom else conditions unset($task_data); } $this-&gt;set('global_task_array', $global_task_array); $this-&gt;response-&gt;type(array('xls' =&gt; 'application/vnd.ms-excel')); $this-&gt;response-&gt;type('xls'); $this-&gt;render('admin_export_excel'); } </code></pre> <p>and my view file is</p> <pre><code> $this-&gt;PhpExcel-&gt;createWorksheet(); $this-&gt;PhpExcel-&gt;setDefaultFont('Calibri', 13); $default = array( array('label' =&gt; __('Task Id'), 'width' =&gt; 'auto'), array('label' =&gt; __('Unique Code'), 'width' =&gt; 'auto'), array('label' =&gt; __('Site Name'), 'width' =&gt; 'auto'), array('label' =&gt; __('Area'), 'width' =&gt; 'auto'), array('label' =&gt; __('Location'), 'width' =&gt; 'auto'), array('label' =&gt; __('Sub Location'), 'width' =&gt; 'auto'), array('label' =&gt; __('About Task'), 'width' =&gt; 'auto') ); $this-&gt;PhpExcel-&gt;addTableHeader($default, array('name' =&gt; 'Cambria', 'bold' =&gt; true)); $this-&gt;PhpExcel-&gt;setDefaultFont('Calibri', 12); foreach ($global_task_array as $index =&gt; $raw) { $data = array( $raw['Task']['id'], $raw['Task']['unique_code'], $raw['Task']['site_name'], $raw['Task']['area'], $raw['Task']['location'], $raw['Task']['sub_location'], $raw['Task']['about_task'] ); $this-&gt;PhpExcel-&gt;addTableRow($data); } $this-&gt;PhpExcel-&gt;addTableFooter(); $this-&gt;PhpExcel-&gt;output('Task-' . date('d-m-Y') . '.xlsx'); </code></pre> <p>I have tried to use the cake afterFilter method for to redirect the user to other action after the excel sheet is generated but its not working.</p> <pre><code>public function afterFilter(){ parent::afterFilter(); if($this-&gt;request-&gt;params['action'] == 'admin_export_excel'){ $this-&gt;redirect(array('controller' =&gt; 'tasks', 'action' =&gt; 'index','admin' =&gt; true)); } } </code></pre> <p>Any help will be appreciated. Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload