CakePHP Clicking PDF Link and View it on New Tab - cakephp

Ok, so I have a web app that uploads a file to the webserver. My input fields in my upload form include: type of upload (dropdown list), title, description, and the file to be uploaded, which is a PDF.
Once the PDF file is uploaded, the download link will appear in another page for the public to see. In addition, the title typed in the input field is the download link.
Now, I want to change my code. Instead of downloading it directly when the link is clicked I want it to open in a new tab, so the users can first look at the PDF file then download it from there.
Here are my codes.
Controller:
public function sendFile(){
$id = $this->request->params['pass'][0];
$staffup = $this->StaffUpload->find('first', array('conditions' => array('iduploads'=>$id)));
$this->response->file($staffup['StaffUpload']['dest'], array('download' => true, 'name' => $staffup['StaffUpload']['title']));
return $this->response;
}
The code above is the download function.
public function resources() {
$this->layout = 'website';
$this->set('staff_uploads', $this->StaffUpload->find('all', array('conditions' => array('type' => 'Resource'))));
}
The code above is the view wherein I show all uploaded files which type is Resources.
View:
<?php
foreach ($staff_uploads as $staff_uploads) {
?>
ul>
<li>
<?php
echo $this->Html->link($staff_uploads['StaffUpload']['title'], array('controller' => 'websites', 'action' => 'sendFile', $staff_uploads['StaffUpload']['iduploads']));
?>
</li>
</ul>
<?php
}
?>
The code above shows the view.
So yeah, back to the question. I want to change the download link to a link in which when clicked, will show the PDF file in a new tab. How do I do that? And by the way, the codes posted above are all working properly. I just want to change my code so that it will be viewed in a new tab when clicked.
Thank you!

According to the docs:
echo $this->Html->link(
'Enter',
'/pages/home',
array('target' => '_blank')
);

Related

CakePHP Open to New Tab on Click

I have a function in my application which the users can upload files to the webserver. Then these uploaded files will appear in another page wherein another type of users can click on the link. Once the link is clicked, a new tab will open and the file will be shown.
But I can't seem to do it. Using the 'target' => '_blank' is not working, or I may have put it on the wrong part of the code.
In my case, when you click on the link, the file will load on the same tab.
Here's my code:
<?php
echo $this->Html->link($staff_uploads['StaffUpload']['title'], array(
'controller' => 'websites',
'action' => 'view',
'target' => '_blank',
$staff_uploads['StaffUpload']['iduploads']
)
);
?>
Thank you in advance!
The correct code is:
<?php
echo $this->Html->link($staff_uploads['StaffUpload']['title'], array(
'controller' => 'websites',
'action' => 'view',
$staff_uploads['StaffUpload']['iduploads']
), array('target' => '_blank')
);
?>
And do read the documentation as burzum has suggested.
Read the documentation.
HTML attribute options go into the 3rd argument of the link() method, not the second which is the URL as string or array.
Problems like this can be simply resolved by using the documentation.

how to rander .html file using cakephp : CakePHP

I am working on CakePHP and I have a URL http://admin.example.com/Pages .
How can I create http://admin.example.com/Pages.html ? Is there any solution or component to solve this issue?
According to CakeBook , You can define extensions you want to parse in routes
E.g. Write the code below in app/Config/routes.php
Router::parseExtensions('html');
This will allow you to send .html extenstion in routes(url)
Not create a link
E.g:
$this->Html->link('Link title', array(
'controller' => 'pages',
'action' => 'index',
'ext' => 'html'
));
When you click that link you will get url in browser something like this
http://admin.example.com/pages/index.html
Do you really need this? CakePHP is automatically render view files from View folder
You can create (if its PagesController and index methond) index.ctp in app/View/Pages folder and it will be automatically render.
You can use file_get_contents function to read files. Something like this:
// in controller
function index() {
$content = file_get_contents('path/to/file.html');
echo $content;
die();
}

title_for_layout while viewing pdf with cakeResponse

i'm viewing pdf files from database using this function:
public function pdfPreview($taskId){
$file = $this->StoredFile->find('first', array(
'conditions' => array(
'StoredFile.id' => $taskId
),
'contain' => false
));
$this->response->body($file['StoredFile']['data']);
$this->response->type('pdf');
//$this->response->header('Content-Disposition', 'inline');
//Return response object to prevent controller from trying to render a view
return $this->response;
}
Is there any way to set title for layout with CakeResponse while not rendering layout, because i'm trying to set filename as title, but i didn't found solution?
Usually the $title_for_layout view var is passed to the html and put between <title> tags.
However, as I understand, you are outputting a PDF using raw data from de database. This is then interpreted by the browser to render a PDF view. The browser thinks it is displaying a file, so when you set the filename in the Content-Disposition header it will display this in its title. Like they do on https://stackoverflow.com/a/2016016/1535656

play media file or open image in browser cakephp

i am using cakephp 2.x i want to play the audio and open image in a browser ....
In my 'view' I have the following code which is correctly showing the filename, and displaying the download link and the file is successfully downloading
<?php echo $this->Html->link('Download', array('controller' => 'bugshot', 'action' => 'download', $files['Audio']['filename']));?>
now i want to play this audio file as well ..as i have two licks on the page ,,, first is download and tthe other is view or play
In my controller I have the following code that is downloading the file
public function download($filename) {
$idUser = $this->Auth->user('idUser');
$folder_url = APP.'uploads/'.$idUser.'/'.$filename;
$this->response->file($folder_url, array('download' => true, 'name' => $filename));
return $this->response;
}
Downloading and serving files is almost the same
Serving files via Cake can be done with one function similiar to the following:
public function download($filename) {
$download = !empty($_GET['download']); // <- example
$idUser = $this->Auth->user('idUser');
$folder_url = APP.'uploads/'.$idUser.'/'.$filename;
$this->response->file($folder_url, array('download' => $download, 'name' => $filename));
return $this->response;
}
That way requesting the url /.../this-file.mp3 will serve the file, whereas the url /.../this-file.mp3?dowload=1 will download it.
Audio is a html5 tag
The simplest way to serve audio, is to just use the audio tag:
<?php
$url = Router::url(array('controller' => 'x', 'action' => 'download', $name));
$download = Router::url(array('controller' => 'x', 'action' => 'download', $name, '?' => array('download' => 1)));
?>
<audio src="<?= $url; ?>" controls>
<!-- alternate content for unsupported case -->
Download Download it;
</audio>
Being a relatively new tag, support isn't universal - see Detailed article on support or other resources for more information on how to handle browsers that do not support this tag.
Or, use one of the many flash based media players that exist.

How to create a static page in cakephp?

Currently am creating one auction website using cakephp. It have a menu bar like about us, contact us. I have created only the default page. So i want to create those pages. advice me how to create.
Old thread, but I found it while trying to do the same in 2.x.
Jack's answer is correct, with a small typo. It should be
Router::connect('/about', array('controller' => 'pages', 'action' => 'display', 'about'));
Hopefully this helps someone else, as it did me.
Create an about.ctp in the /app/views/pages/ folder.
Then add Router::connect('/about', array('controller' => 'pages', 'action' => 'display', 'about')); in the /app/config/routes.php file. You should be able to access it at www.yoursite.com/about
Since the new version of cakephp is freshly out, I'm adding this answer to deal with the newer version (3.x).
To link to a static page you still use the PageController but the code slightly changed.
Here the code you would need in the 3.x version
$routes->connect('/about', ['controller' => 'Pages', 'action' => 'display', 'about']);
You can read more about the new routing system here.
I have no affiliation with cakephp. I added this answer since I found this post while searching how to do this in 3.0
Read more here
Method 1: if you want to create content pages like about us, privacy policy which contents can be changed by an admin interface follow these steps
Step1: Change pagesController
class PagesController extends AppController {
function beforeFilter() {
$this->Auth->allow('content');//to allow to be visible for non-logged in users, if you are using login system
parent::beforeFilter();
}
public function content($id = null, $layout = null, $theme=null) {
if ($layout) $this->layout = $layout;//if you are using mulitple layouts and themes and want to change it dynamicaly
if ($theme) $this->theme = $theme;
$this->set('content', $this->Page->find('first', array('conditions' => array('Page.id' => $id))));
$this->Page->id= $id;
$this->set('title_for_layout', $this->Page->field('title'));
}
}
Step 2: add a table content with fields you need like id, title, content, image, theme,layout etc.
Step 3: In View/Pages add content.ctp
<div class="row innerPage">
<div class="col-lg-12 col-md-12 col-sm-12">
<div class="row userInfo">
<div class="col-xs-12 col-sm-12">
<h1 class=" text-left border-title"> <?php echo $content['Page']['title'];?> </h1>
<div class="w100 clearfix">
<?php echo $content['Page']['content'];?>
</div>
</div>
</div>
However you can change html according to your need, I prefer bootstrap framework.
Then you can use it as
<?php echo $this->html->link("Terms of Services", array("controller" => "pages", "action" => "content", 5), array("class" => 'themeprimary','target'=>'_blank')) ?>
This will generate a link yoursite/pages/content/5. 5 is the id of row you want to show the details of.
If you want your link like yoursite/terms then you need one more step to go. In routes.php add this line.
Router::connect('/terms', array('controller' => 'pages', 'action' => 'content',5));
Method 2: You simply need to display content without any database
Step1 :Just create a about.ctp under View/Pages and put the content you want to display
Step 2: Change your pagesController. add a method about
public function about($layout = null) {
$this->set('title_for_layout', 'About');
}
Thats it.
You can use the pages controller for this purpose.
Creating views at APP/views/pages/ with names such as about_us.ctp and contact_us.ctp will allow you to access them at the url:
www.site.com/pages/about_us
you can then change how these URIs look with routing.

Resources