cakephp 3.0 $this->output function does not exist error - cakephp

I am using the image helper of 2.x in my cakephp 3.x application but i am facing a problem with this that in my cakephp 2.x the function return the value as below code
return $this->output(sprintf($this->Html->_tags['image'], $this->webroot($relfile), $this->Html->_parseAttributes($htmlAttributes, null, '', ' ')), $return);
but in cakephp 3.x output and _parseAttributes function is removed so i dont know how to use these two function in cakephp 3.x
_parseAttributes function contain the image related data like 'class', 'alt' other things
i have searched a lot on net for output function in cake 3.x but did not find any successfull solution please try to help me
Thanks

Use the Html helper
There's nothing in your example code that justifies the use of custom code for generating the tag, so you could simply use the Html helper instead
// ...
class YourCustomHelper extends Helper
{
public $helpers = [
'Html',
// ...
];
public function someMethod($relfile, array $htmlAttributes)
{
$options = $htmlAttributes + ['pathPrefix' => false];
return $this->Html->image($url, $options);
}
// ...
}
Note the use of the pathPrefix option, since it looks like you want to point to a custom path, you should disable it, as otherwise you may end up with the default image base URL being prepended to the path.
Custom tags and attributes
If you'd really need a custom solution that isn't already covered by other helpers, then the replacement for output() and _parseAttributes() are return and string templates.
For the sake of supplying an example, here's a trimmed down one of what HtmlHelper::image() does:
use Cake\Core\Configure;
use Cake\View\StringTemplateTrait;
// ...
class YourCustomHelper extends Helper
{
use StringTemplateTrait;
protected $_defaultConfig = [
'templates' => [
'someTemplate' => '<img src="{{url}}"{{attrs}}/>',
],
// ...
];
public $helpers = [
'Url',
// ...
];
public function someMethod($relfile, array $htmlAttributes = [])
{
$url = $this->Url->assetUrl($relfile);
$templater = $this->templater();
return $templater->format('someTemplate', [
'url' => $url,
'attrs' => $templater->formatAttributes($htmlAttributes),
]);
}
// ...
}
This should be pretty much self explantory, you include the StringTemplateTrait that holds the templating functionality, define a custom template, and format it using the templater.
Note the use of the Url helper, it will do all the necessary stuff like encoding the URL, timestamping it, adding the webroot path, etc...
See also
Cookbook > Views > Helpers > Configuration options
Source > \Cake\View\Helper\HtmlHelper::image()
API > \Cake\View\Helper\UrlHelper::assetUrl()
API > \Cake\View\StringTemplate::format()
API > \Cake\View\StringTemplate::formatAttributes()
Cookbook > Appendices > 3.0 Migration Guide > Helpers

Related

Respond as XML not working since cakePHP 3.1

I need to render an XML+XSL template in my application, and it used to work with cakePHP 3.0. I have made the switch to 3.1 recently and it has stopped working. The problem is that I was having a formatted view of my XML, while now I just get a plain string.
The migration guide says something about some changes in the RequestHandlerComponent, but nothing helpful (or maybe it's just me and I don't get the point :)).
This is my controller (it is exactly as it was with Cake3.0):
<?php
namespace App\Controller;
use App\Controller\AppController;
use Cake\Utility\Xml;
use Cake\Event\Event;
use Cake\Routing\Router;
use Cake\ORM\TableRegistry;
use Cake\Filesystem\Folder;
use Cake\Filesystem\File;
use Cake\Network\Email\Email;
use Cake\Core\Configure;
use Cake\I18n\Time;
/**
* Invoices Controller
*
* #property App\Model\Table\InvoicesTable $Invoices
*/
class InvoicesController extends AppController
{
public $components = [
'Browser',
'Reorder11'
];
public $helpers = [
'Multiple'
];
public $paginate = [];
public function initialize()
{
parent::initialize();
$this->loadComponent('Paginator');
$this->loadComponent('RequestHandler');
}
public function beforeFilter(Event $event)
{
parent::beforeFilter($event);
$this->Auth->allow(['demo']);
}
/*
* ... several other functions ...
*/
public function viewxml($id = null)
{
$this->viewBuilder()->layout('xml');
$invoice = $this->Invoices->myInvoice($id, $this->Auth->user('id'));
$this->RequestHandler->respondAs('xml');
$this->set('invoice', $invoice);
}
}
The xml.ctp layout, which is really simple
echo $this->fetch('content');
and the viewxml.ctp template just echoes the xml as a string.
How can I obtain the formatted XML+XSL again?
Try add: $this->response->header(['Content-type' => 'application/xml']);
I had the same error but my output was pdf
working 3.0.14 using this code:
$this->RequestHandler->respondAs("pdf");
$this->layout = 'pdf/default';
$this->view = 'pdf/report1_pdf';
for 3.1.x (this works if u save the file and open later, if you try to open it directly on browser its print the plain file content as a txt/html):
$this->viewBuilder()->layout('pdf/default');
$this->viewBuilder()->template('pdf/report1_pdf');
$this->RequestHandler->respondAs('pdf');
$this->response->header(['Content-type' => 'application/pdf']);

Plugin route to a prefixed controller

I'm creating a plugin for my application (using CakePHP 2.6.0) that allows users to login into a user area using the same model as for the admin area, so I'm trying to get the same type of URI scheme as the admin area e.g. /admin/users/login but then for /special/users/login. I have the following route in my plugin's Config/routes.php and added the 'special' prefix to the 'Routing.prefixes' configuration:
Router::connect('/special/:controller/:action', array(
'special' => true,
'prefix' => 'special',
'plugin' => 'Special',
'controller' => ':controller',
'action' => ':action',
));
With above route and entering the following url /special/users/login it would make sense to me right now if Cake went for Plugin/Controller/SpecialUsersController.php (considering namespace conflicts with the main application's UsersController) but instead I get an error Error: Create the class UsersController.
Is there a built-in way for it to load a prefixed controller (without changing the url) based on the plugin? Or is there a better way to neatly extend my main application? Am I going about this the wrong way?
I could not find a built-in way for it to work as I wanted to so I changed the way URL strings were parsed using a custom route class in my app's Routing/Route/ folder:
App::uses('CakeRoute', 'Routing/Route');
/**
* Plugin route will make sure plugin routes get
* redirected to a prefixed controller.
*/
class PluginRoute extends CakeRoute {
/**
* Parses a string URL into an array.
*
* #param string $url The URL to parse
* #return bool False on failure
*/
public function parse($url) {
$params = parent::parse($url);
if($params && !empty($params['controller']) && !empty($params['plugin'])) {
$params['controller'] = $params['plugin'] . ucfirst($params['controller']);
}
return $params;
}
}
And then setting up my plugin routes as:
App::uses('PluginRoute', 'Routing/Route');
Router::connect('/special/:controller/:action', array(
'special' => true,
'prefix' => 'special',
'plugin' => 'Special',
'controller' => ':controller',
'action' => ':action',
), array(
'routeClass' => 'PluginRoute'
));
This resulted in /special/users/login creating the controller I wanted Plugin/Controller/SpecialUsersController.php, no side effects so far. Extending the main application's UsersController is a different story though.
Maybe someone else has use of this or knows a better solution?

CakePHP adding language parameter to URLs

I implemented the solution at this article.
"beforeFilter" and "_setLanguage" works fine. If URL has language parameter, I can successfully set cookies/session variables. And use it between controllers.
That solution also includes adding url() function to AppHelper.
class AppHelper extends Helper {
function url($url = null, $full = false) {
if(!isset($url['language']) && isset($this->params['language'])) {
$url['language'] = $this->params['language'];
}
return parent::url($url, $full);
}
}
But I have many URLs in my View files that are written without using HtmlHelper. Like this:
// myfile.ctp
<a href="/mypage/">click me plase<a>
So it seems like AppHelper::url solution doesn't fix my issue. What would be better alternative to add language prefix to URLs?
I thought defining a global variable like this:
// AppController::_beforeFilter()
if ($this->request->params['language'] != "")
{
Configure::write('URLprefix', '/'.$this->request->params['language']);
} else {
Configure::write('URLprefix', '');
}
Then change view file like this:
// myfile.ctp
<a href="<?php echo URLprefix; ?>/mypage/">click me plase<a>
But it doesn't seem a good way. Is there a better way to add prefix to URLs. Or should I add to all links by hand ?
Related:
Adding a prefix to every URL in CakePHP
CakePHP 2.x i18n route
CakePHP 2.1 URL Language Parameter
You should generate all links and URLs within your application using HtmlHelper::link() and HtmlHelper::url()
This will make sure that your Routes are taken into account when generating URLs (Reverse routing)
For example, if you decide to define a 'friendly' URL Route /logout for /users/logout, then this:
echo $this->Html->link('Log out', array(
'controller' => 'users',
'action' => 'logout'
));
Will create this link:
<a href='/logout'>Log out</a>
If you later decide to modify the 'friendly' URL Route (/sign-out) for the logout URL, then all links in your application will automatically be adjusted.
The same call to the HtmlHelper, will now output:
<a href='/sign-out'>Log out</a>
Read more on this subject here:
Reverse Routing

CakePHP - Using the RSS Helper without RequestHandler Component

I have set up the RSS Helper (with CakePHP 1.3.4) and all is working - I can access my feeds by /news/feed.rss - as exampled at http://book.cakephp.org/view/1461/Creating-an-RSS-feed-with-the-RssHelper
But I want to be able to do this conditionally, in sudo, something like:
if (!empty($var)) {
switch ($var) {
case one :
$xml = $this->method->find('$var conditions...');
... use RSS Helper to serve results as XML.
case two :
$xml = $this->method->find('other $var conditions...');
... use RSS Helper to serve results as XML.
}
}
Can I use the RSS Helper in this circumstance? What calls/syntax do I use?
There are 2 ways to do this.
Basically you can pass a variable like this:
http://yourserver.com/news/feed.rss?recent=20
and then in the controller you can access this variable with
$this->params['url']['recent']; //20
Or you can add a line in your Router file like this:
Router::connect('/feed-:recent/*', array('plugin'=>false, 'controller' => 'news', 'action' => 'feed'), array('recent'=>'[0-9]+'));
This way your url will look like:
http://yourserver.com/news/feed-20.rss
and finally I believe that url like this will work as well:
http://yourserver.com/news/feed.rss/recent:20
You can pass params to the function the same as any other controller call.
IE
public function rss( $limit = 20, $topic = null ){
if( !$topic ){
$xml = $this->Article->find( 'all', array( 'limit' => $limit ));
...
} else {
$xml = $this->Article->find( 'all', array( 'limit' => $limit, 'conditions' => array( 'Article.topic' => $topic )));
}
...
}
}
?>
Then you can access the rss feed with: http://domain/articles/rss.rss
If you need to pass parameters: http://domain/articles/rss/100.rss (fetch all with 100 results)
Or like this: http://domain/articles/rss/10/obama.rss (fetch all with the topic "obama" and return 10 items.
The RequestHandler allows you to use a url like /posts/index.rss and it automatically loads the RSS Helper and sends the output to /views/posts/rss/index.ctp, and uses layout at /views/layouts/rss/default.ctp.
But if you want to use the RSS Helper on it's own you need to:
1) include it the controller
var $helpers = array('Rss');
2) in your controller action you need to specify where the output goes, so
$this->render('/posts/rss/index','/rss/default');
In this case I also specified which layout to use in the 2nd argument. The first arg - the index.ctp file location is relative to your views/ directory. The second arg - the layout is relative to your views/layouts/ directory.
So I have a method in my posts_controller that I use to identify which feed is wanted (through a passed var) and subsequently finds the posts and sends them to the correct views and layouts:
function rss(){
if (!empty($this->params['pass'])){
$ops=array(
'conditions'=>'where feedname_id=' . $this->params['pass'][0],
'order' => 'Post.created DESC',
'limit' => 10
);
$this->set('posts', $this->Post->find('all',$ops));
$this->render('/posts/rss/index','/rss/default');
} else {
$this->redirect(array('controller'=>'feednames','action'=>'index'));
}
}
There might be better ways to code this - if so please let me know.

CakePHP routing with colon separator

I need to create routes that include a colon to produce URLs like http://app.com/prjct:a9b5c. Obviously it's currently simple to use a slash instead with the default routing.
$SLUG = array('slug' => '[-_A-Za-z0-9]+');
Router::connect('/prjct/:slug', array('controller' => 'projects', 'action' => 'show'), $SLUG);
But routes specifications use the colon character as a special indicator, which interferes with my naive attempt to replace the second slash above with another colon.
How do I use colons in this case for a route?
You can use named parameter as explained in CakePHP Cookbook. Write code below in your app/config/routes.php:
// Parse only the 'prjct' parameter if the current action is 'show' and the controller is 'projects'.
Router::connectNamed(array('prjct' => array('action' => 'show', 'controller' => 'projects')));
// Then set default route to controller 'projects' and action 'show
Router::connect('/', array('controller' => 'projects', 'action' => 'show'));
In your projects_controller.php :
function show(prjct = null) {
// Check if prjct match the pattern
$pattern = '[-_A-Za-z0-9]+';
if(!preg_match($pattern, prjct)){
// Redirect somewhere else
}
// Rest of your code here
}
I think this is indeed out of scope for simple routes. I see two options:
Use a custom route parsing class, as described here. There isn't a whole lot of documentation on the topic, but you can extend the existing class and play around with it to get a hang of what it's doing. Then customize it to your needs.
class MyRoute extends CakeRoute {
public function parse($url) {
debug($url); // input
$route = parent::parse($url);
debug($route); // output
return $route;
}
}
Route these URLs with a catch-all route to a controller, where the parameter will be available as a named parameter in $this->params['named']. Do what you need to do there.

Resources