1

Any one can tell me how to view a PHP page in a new tab from CodeIgniter 4 controller. I tried this code:

return view('/pdfrepo/resume-page-pdf',$data, array('target' => '_blank'));

the page is viewed with no error at all, but on the same tab!.

Shubair
  • 19
  • 1
  • 4

2 Answers2

1

rendering a view is not the same as clicking on a link:

rendering a view: if you look at the docs Global Functions and Constants - view you'll see that third "options" parameter you are using is meant for a completely different concept as you try to apply it to, like the styling of an <a href>, which in your example doesn't even exist (yet):

$options (array) – An array of options that will be passed to the rendering class. Currently, these options are available for use within the $options array: saveData, cache, debug

clicking a link: check the documentation URL Helper - anchor where you would use the attributes parameter to add something like: ['target' => 'blank'] to achieve a link which opens a new tab

P.S. _blank: usually a new tab, but users can configure browsers to open a new window instead.

Vickel
  • 7,879
  • 6
  • 35
  • 56
  • Thank you for your response. What about passing the $data variable? – Shubair Jul 23 '22 at 09:31
  • you could use [http_build_query](https://www.php.net/manual/en/function.http-build-query.php), see this answer: https://stackoverflow.com/a/45339078/2275490 or [curl](https://www.php.net/manual/en/book.curl.php) – Vickel Jul 23 '22 at 12:35
1

PHP can't open a new tab and display the data. PHP is a Server Side Language. for this, you have to use a Client Side Language such as JavaScript.

In your main view

<a href="<?= base_url('controller/route') ?>" target="_blank">View PDF</a>

target="_blank" which performs the miracle that loads the page in a new tab.

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
  • Thank you dear for your responses. The ['target'='_blank'] performs the miracle as you said but I need also to pass multi-dimension array, i.e. $data variable. – Shubair Jul 23 '22 at 09:30
  • 1
    @Abdulla congrats to your Codeigniter Gold Badge, 3rd user ever to reach that badge! well done... – Vickel Jul 23 '22 at 19:30