I use themes on my cakePHP 2.6 project and would like to test my application.
I have a view file in /app/View/Pages/view.ctp
I overwrite it here /app/View/Themed/Theme/Pages/view.ctp
How can I change the $this->theme to get the result of the themed view?
I tried this (without result):
public function testView() {
$this->theme = 'Theme';
$url = Router::url(array('controller' => 'Pages', 'action' => 'view'));
$options = array(
'return' => 'contents'
);
$result = $this->testAction($url, $options);
$this->assertNotEmpty($result);
}
Related
I am developing a site in cakephp2.5. I have two plugin Webmaster and debugKit. When I write
CakePlugin::load('Webmaster', array('bootstrap' => false, 'routes' => false));
CakePlugin::load('webmaster');
CakePlugin::load( 'DebugKit');
The site works correctly on local system but not on live server. However if I remove one of the above Webmaster it shows error on local system and also on live
Error: The application is trying to load a file from the webmaster plugin
Error: Make sure your plugin webmaster is in the app\Plugin directory and was loaded
I also tried but no luck. Struggling from 2 days. Also seen these links
link1
link2
Here is my WebmasterAppController
<?php
App::uses('AppController', 'Controller');
class WebmasterAppController extends AppController{
public $theme= "beyond";
//public $layout=NULL;
public function beforeFilter(){
//$this->Auth->allow('login');
$this->Auth->loginAction= array('controller'=>'users', 'action'=>'login');
$this->Auth->loginRedirect= array('controller'=>'users', 'action'=>'index');
$this->Auth->loginError= 'Invalid Email/Password!';
$this->Auth->authError= 'You are not authorised to access!';
$this->Auth->logoutRedirect= array('controller'=>'users', 'action'=>'login');
AuthComponent::$sessionKey = 'Auth.Webmaster';
//we don't need to load debug kit
$this->Components->unload('DebugKit.Toolbar');
parent::beforeFilter();
}
}
And here is AppController
class AppController extends Controller{
public $cakeDescription = "CakePhp | ";
public $theme = "alpus";
public $ext = 'html';
public $helpers = array('NiceForms', 'coolFun');
public $components = array(
'DebugKit.Toolbar',
'Common',
'Session',
'Auth' => array(
'loginRedirect' => array(
'controller' => 'pages',
'action' => 'dashboard'
),
'logoutRedirect' => array(
'controller' => 'users',
'action' => 'login',
),
'authenticate' => array(
'Form' => array(
'passwordHasher' => 'Blowfish',
'fields' => array('username' => 'email')
)
),
'sessionKey' => 'Admin'
)
);
public function beforeFilter(){
if (isset($this->params['prefix']) && $this->params['prefix'] == 'admin') {
$this->theme = 'smart';
$this->Auth->loginAction = array('controller' => 'users', 'action' => 'admin_login', 'plugin' => false);
}
$this->Auth->allow('register');
}
}
Edit 1:
For cakephp 3.0 we can use
public function beforeFilter(Event $event) {
$this->Auth->sessionKey ='Auth.User';
}
in AppController.php to set different sessionKey for frontend and backend.
Why do you have multiple load calls for the same plugin anways? There should be only one per plugin!
That being said, mind your casing, the second CakePlugin::load() call uses webmaster instead of Webmaster. Plugin names should start with an uppercased letter, just like the corresponding directory name.
Your local filesystem is most probably case insensitive, so it can find the plugin directory even though the casing doesn't match.
Update It looks like I intially got you wrong, if CakePHP would tell you to load the plugin webmaster without you already having added a CakePlugin::load('webmaster') call, then you must have used the lowercased webmaster somewhere else in your code.
I'm using CakePHP v2.42 & would like to have SEO friendly URL in page pagination.
My current pagination is like
http://www.website.com/ubs/page/page:2
What to do to change to
http://www.website.com/ubs/page/2
My Controller is
<?php
class UbsController extends AppController {
public $helpers = array('Html', 'Form');
public function index() {
$this->paginate = array(
'limit' => 100,
);
$ubs = $this->paginate();
$this->set('ubs', $ubs);
}}
My Router is
Router::connect('/ubs', array('controller' => 'ubs', 'action' => 'index'));
Router::connect('/ubs/page/*', array('controller' => 'ubs', 'action' => 'index'));
EDIT - ADD MORE QUESTION
Answer by #kicaj is perfectly correct for the Router & Controller. However, the navigation link only display correctly on the first page.
In the first page navigation link show like this which is correct
http://www.website.com/ubs/
http://www.website.com/ubs/page/2/
http://www.website.com/ubs/page/3/
But navigation link show like this in second/third page page
http://www.website.com/ubs/index/2/
http://www.website.com/ubs/index/2/page:3/
I guess need to edit index.ctp file but not sure what to do.
My current navigation link in index.ctp show like this
$paginator = $this->Paginator;
$paginator->prev("« Prev");
$paginator->numbers(array('modulus' => 200, 'separator' => ' '));
$paginator->next("Next »");
What to change to correct this
Try this:
Router::connect('/ubs/page/:page', array(
'controller' => 'ubs',
'action' => 'index'
), array(
'pass' => array(
'page'
),
'page' => '[\d]+'
));
and in index action in ubs controller add code bellow:
public function index($page = 1) {
// ...
$this->request->params['named']['page'] = $page;
// ...
}
In your Paginator Helper you can choose the right friendly url you want with setting some options
url The URL of the paginating action. ‘url’ has a few sub options as well:
sort The key that the records are sorted by.
direction The direction of the sorting. Defaults to ‘ASC’.
page The page number to display.
There here an example .
$this->Paginator->options(array(
'url' => array(
'sort' => 'email', 'direction' => 'desc', 'page' => 6,
'lang' => 'en'
)
));
the source : Modifying the options PaginatorHelper uses
I read the documentation regarding file downloads, however I can't seem to get this to work.
I have read through questions here as well, and have had no luck.
My function looks as follows:
public function generate($id) {
$this->layout = 'ajax';
$this->Magazine->recursive = 2;
$DistributionLists = $this->Magazine->DistributionList->find('all',
array(
'conditions' => array(
'Magazine.id' => $id
),
'order' => array(
'DistributionList.priority ASC'
)
)
);
$this->set('magazine',$DistributionLists[0]['Magazine']['magazine_name']);
$this->set(compact('DistributionLists'));
}
public function download() {
$this->viewClass = 'Media';
$params = array(
'id' => "Magazine Distribution List.doc",
'name' => "Magazine Distribution List",
'download' => true,
'extension' => 'doc',
'path' => APP . "tmp" . DS
);
$this->set($params);
unlink(APP."tmp".DS);
$this->redirect(array('action'=>'index'));
}
public function afterFilter() {
parent::afterFilter();
if($this->action == 'generate') {
$this->redirect(array('action'=>'download'));
}
}
The reason I have an afterFilter function is because the word document that needs to be downloaded is created in the view file.
Does anyone know why this doesn't work?
You have to remove the call to the redirect method in your download method because it prevents your view from getting "rendered" due to the redirect.
Hi all I'm currently using cake 2.1, I am trying to get a page to render as a pdf in the browser however I believe I'm not loading the engine correctly. I believe it is because I'm not loading anything in routes.php.
here is the relevant code in routes.php
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
/**
* ...and connect the rest of 'Pages' controller's urls.
*/
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
/**
* Load all plugin routes. See the CakePlugin documentation on
* how to customize the loading of plugin routes.
*/
CakePlugin::routes();
Router::mapResources(array('Invoices'));
/**
* Load the CakePHP default routes. Remove this if you do not want to use
* the built-in default routes.
*/
require CAKE . 'Config' . DS . 'routes.php';
here is boostrap.php
<?php CakePlugin::load('DebugKit');
CakePlugin::load('CakePdf', array('bootstrap' => true, 'routes' => true));
CakePlugin::loadAll();
here is the function in the controller
public function view($id = null) {
$this->set('title_for_layout', 'Invoices');
$this->set('stylesheet_used', 'homestyle');
$this->set('image_used', 'eBOXLogoHome.png');
$this->layout='adminpdf';
Configure::write('CakePdf', array(
'engine' => 'CakePdf.WkHtmlToPdf',
'download'=>true,
'binary'=>'C:\Program Files (x86)\wkhtmltopdf\wkhtmltopdf.exe'));
$this->pdfConfig = array('engine' => 'CakePdf.WkHtmlToPdf');
$this->Invoice->id = $id;
if (!$this->Invoice->exists()) {
throw new NotFoundException(__('Invalid invoice'));
}
$this->pdfConfig = array(
'orientation' => 'potrait',
'filename' => 'Invoice_' . $id
);
$this->set('invoice', $this->Invoice->read(null, $id));
//Retrieve Account Id of current User
$accountid=$this->Auth->user('account_id');
//Find all Invoices where $conditions are satisfied
$invoicedetails=$this->Invoice->find('first', array(
'conditions' => array('Invoice.id'=>$id)));
//prints fieldsInvoice details, including invoice and field information
$invoices=$this->FieldsInvoice->find('all',array(
'conditions'=>array(
'invoice_id'=>$id)));
$itemInvoice=$this->InvoicesItem->find('all',array('conditions'=>array('invoice_id'=>$id)));
//Set variables
$this->set('invoicedetails', $invoicedetails);
$this->set('invoice', $invoices);
$this->set('accountid', $accountid);
$this->set('itemInvoice', $itemInvoice);
}
I am using this method of loading the pdf - https://github.com/ceeram/CakePdf/
and am using this engine wkhtmltopdf
I have been stuck on this for several days so any help would be greatly appreciated.
you use WkHtmlToPdf you need to download that engine because its not default.
check here : app/plugin/cakepdf/Vendor.
you see default : dompdf , mpdf and tcpdf.
i use DomPdf.
in your bootstrap.php load the engine like this :
Configure::write('CakePdf', array(
'engine' => 'CakePdf.DomPdf',
'pageSize'=>'A4',
'orientation' => 'landscape',
));
and use inside your controller :
$this -> pdfConfig = array (
'orientation' => 'landscape',
'download' => true,
'filename' => 'Invoice_' . $id
);
you can also take a look at : http://www.slideshare.net/jellehenkens/building-php-documents-with-cakepdf-cakefest-2012
good luck i hope this will help.
i have an issue trying to generate rss. I followed all the steps of http://book.cakephp.org/1.3/en/view/1460/RSS, but when i try in the url my index.rss shows me only my index page not in xml format.
this is my index of post_Controller:
var $components = array('Session','RequestHandler');
var $helpers = array('Html','Form','Time','Text');
function index() {
if( $this->RequestHandler->isRss() ){
$posts = $this->Post->find('all', array('limit' => 20, 'order' => 'Post.created DESC'));
$this->set(compact('posts'));
}
$this->set('title_for_layout', 'mi blog');
$this->Post->recursive = 1;
$this->set('posts', $this->paginate());
}
this is my layout in app/views/layouts/rss/default.ctp:
echo $this->Rss->header();
if (!isset($documentData)) {
$documentData = array();
}
if (!isset($channelData)) {
$channelData = array();
}
if (!isset($channelData['title'])) {
$channelData['title'] = $title_for_layout;
}
$channel = $this->Rss->channel(array(), $channelData, $content_for_layout);
echo $this->Rss->document($documentData,$channel);
this the view in app/views/posts/rss/index.ctp
$this->set('documentData', array(
'xmlns:dc' => 'http://purl.org/dc/elements/1.1/'));
$this->set('channelData', array(
'title' => __("Articles", true),
'link' => $this->Html->url('/', true),
'description' => __("Articulos mas recientes.", true),
'language' => 'en-us'));
// content
foreach ($posts as $post) {
$postTime = strtotime($post['Post']['created']);
$postLink = array(
'controller' => 'posts',
'action' => 'view',
$post['Post']['id']);
// You should import Sanitize
App::import('Sanitize');
// This is the part where we clean the body text for output as the description
// of the rss item, this needs to have only text to make sure the feed validates
$bodyText = preg_replace('=\(.*?\)=is', '', $post['Post']['body']);
$bodyText = $this->Text->stripLinks($bodyText);
$bodyText = Sanitize::stripAll($bodyText);
$bodyText = $this->Text->truncate($bodyText, 400, array(
'ending' => '...',
'exact' => true,
'html' => true,
));
echo $this->Rss->item(array(), array(
'title' => $post['Post']['title'],
'link' => $postLink,
'guid' => array('url' => $postLink, 'isPermaLink' => 'true'),
'description' => $bodyText,
'pubDate' => $post['Post']['created']));
}
which could be the problem ... Also i have put the component in the app_controller.php
var $components = array ('Auth', 'Session', 'RequestHandler');
but nothing happens the index.rss is the Same of posts / index
It looks like you are not returning the RSS view in the index controller. Update the RSS section of the index function to return the rss view:
function index() {
if( $this->RequestHandler->isRss() ){
$posts = $this->Post->find('all', array('limit' => 20, 'order' => 'Post.created DESC'));
return $this->set(compact('posts'));
}
// ...snip...
}
UPDATE
It's how Chrome handles the layout. I know it is horrible. FireFox and IE handle RSS layout much better. But you can install the RSS Layout extension for Chrome and it will format it the same way.
https://chrome.google.com/extensions/detail/nlbjncdgjeocebhnmkbbbdekmmmcbfjd
In the action of the controller. You must add
$this->response->type("xml");
at the end.