Monday, October 28, 2013

Re: Automatically Serialize All View Variables

Why not making this a clean component or custom View class, e.g. SerializeView etc?
Hacking this in the AppController (which should stay lean) doesnt sound right to me.
Also, re-usability is not very good this way.

And don't use private, use protected :P


Am Montag, 28. Oktober 2013 01:50:38 UTC+1 schrieb Matt Bowden:
No question here, just thought I'd share a solution to a problem I needed to solve.

I'm working on a decoupled application where the back-end is all CakePHP and the front-end is all Javascript. The front-end communicates with the back-end as a RESTful API. I could write a handful of posts in here about some of the problems I ran into, but right now, I just want to share a trick I developed with serializing view variables.

I use jQuery AJAX functions to get json from CakePHP. I got tired of writing this all over my controllers:

$this->set('_serialize', array('omg', 'so' 'many', 'view', 'vars'));

So I decided to automatically serialize all my view variables in my app controller's beforeRender callback. (beforeRender is called after all the application logic is finished, right before the view is rendered. Good name for it huh?)

// AppController.php
public function beforeRender(){
// When setting _serialize, you pass in a list of variable names.
// array_keys($this->viewVars) returns an array of the names of the view variables 
$this->set('_serialize', array_keys($this->viewVars));
}

That works, but if I need a way to ignore some view variables, I would have to do something a little more elaborate.

// AppController.php
public function beforeRender(){
// When setting _serialize, you pass in a list of variable names.
// array_keys($this->viewVars) returns an array of the names of the view variables
$viewVars = array_keys($this->viewVars);
 
// I include _serialize to prevent the possibility of a _serialize inside another _serialize 
$varsToIgnore = array('_serialize', 'debugToolbarPanels', 'varThatOnlyAppliesToBackEnd');
 
foreach($varsToIgnore as $varToIgnore){
// Might be a better way of doing this, but it works and seems safe
// in_array checks if a value exists in an array
// unset removes an array or array index in this case
// array_search returns the index of where a value is found 
if(in_array($varToIgnore, $viewVars)) unset($viewVars[array_search($varToIgnore, $viewVars)]);
}
 
// Reindex the viewVars array after removing variables from above
// I don't know if this is really necessary, but I did it anyway.
$viewVars = array_values($viewVars);
 
$this->set('_serialize', $viewVars);
}

My beforeRender method is getting a little too long for my liking, so I decided to clean it up a bit, by moving the variables to ignore into a private property and the logic to serialize the view variables into its own private method.

// AppController.php
// A shorter name would be easier to type
private var $viewVariablesNotToSerialize = array('_serialize', 'debugToolbarPanels', 'varThatOnlyAppliesToBackEnd');

public function beforeRender(){
$this->__serializeViewVars();
}

private function __serializeViewVars(){
// When setting _serialize, you pass in a list of variable names.
// array_keys($this->viewVars) returns an array of the names of the view variables
$viewVars = array_keys($this->viewVars);
 
foreach($this->__viewVarsNotToSerialize as $varToIgnore){
// Might be a better way of doing this, but it works and seems safe
// in_array checks if a value exists in an array
// unset removes an array or array index in this case
// array_search returns the index of where a value is found 
if(in_array($varToIgnore, $viewVars)) unset($viewVars[array_search($varToIgnore, $viewVars)]);
}
 
// Reindex the viewVars array after remove variables from above
// I don't know if this is really necessary, but I did it anyway. 
$viewVars = array_values($viewVars);
 
$this->set('_serialize', $viewVars); 
}

I hope someone finds this useful.

Matt Bowden
Web Application Developer
Skybrick

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.

Sunday, October 27, 2013

Re: Defining Plugin

You're absolutely right, I was definitely confusing the two. I suppose that I'm so used to referencing the plugin as camel-case everywhere else within the app and since functions are camel-case and they are referenced as such in the URL, it wasn't a far stretch to make the mistake of constructing my links with 'plugin' => 'SomePlugin'. I definitely learned my lesson on that one. I have everything fixed in the app except for pagination. It loads the first :page fine but still assumes on the following :pages the plugin should be loaded as camel-case instead of underscore. Is there something I missed when changing everything over, or is there some place I should let Cake know that those pages belong in some_plugin? I noticed that when the error is thrown the error log shows the plugin as null.  



On Sun, Oct 27, 2013 at 6:56 PM, euromark <dereuromark@gmail.com> wrote:
Yes, I guess you have been creating non-conventional links so far. Mainly because you are confusing framework internal naming schemes with URL naming scheme
The latter defines everything underscore_lowercased, even the plugin.
The first refers mainly to classes, which will always be named CamelCased.

You need to understand the difference in order to not write faulty code - which at some point can blow up (as it then did).


Am Samstag, 26. Oktober 2013 04:43:11 UTC+2 schrieb CrotchFrog:
I've noticed recently some strange behavior when it comes to naming conventions and Plugins.  

For example if I create a link somewhere within a plugin ie. $this->Html->link('Some Link', array('controller' => 'controller', 'action' => 'action')); 
Following the link would land you on the page app/plugin/controller/action unless a location is specified outside of the current plugin and controller. 

Links created this way and that have been working since they were created sometimes out of the blue throw an error along the lines of SomePluginController cannot be found. 
This is strange because the link has always worked in the past. When these links create an error I have to re-create the link and specify the plugin $this->Html->link('controller' => 'controller', 'action' => 'action', 'plugin' => 'some_plugin')
I have to use an underscore in the plugin name, using camel case results in the same error. 

It's possible that some, but not all, of the links were created only specifying the action and not the controller. With this being the case I could sort of understand the resulting error but not when I have specified both the controller and the action.

I created all of my custom routes with the plugin as camel case, it just seems to be links and redirects that display this behavior.  

I assumed that with sticking to naming conventions I could write it as SomePlugin or some_plugin and either one would be fine. 

Have I been creating my links incorrectly this entire time? 

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to a topic in the Google Groups "CakePHP" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cake-php/zDwuUhlVGzE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.

Saving CakeEmail output to .eml

Hi

I have a client requirement to save generated emails to disk, as well as send them.

I can see that the CakeEmail::send() function returns an array, holding the headers and the message.

Has anyone had any experience in saving this data to an .eml file, or any other client email file format, where the user could be expected to open the file later and see the email, and attachments, etc.

Regards
Reuben Helms

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.

Automatically Serialize All View Variables

No question here, just thought I'd share a solution to a problem I needed to solve.

I'm working on a decoupled application where the back-end is all CakePHP and the front-end is all Javascript. The front-end communicates with the back-end as a RESTful API. I could write a handful of posts in here about some of the problems I ran into, but right now, I just want to share a trick I developed with serializing view variables.

I use jQuery AJAX functions to get json from CakePHP. I got tired of writing this all over my controllers:

$this->set('_serialize', array('omg', 'so' 'many', 'view', 'vars'));

So I decided to automatically serialize all my view variables in my app controller's beforeRender callback. (beforeRender is called after all the application logic is finished, right before the view is rendered. Good name for it huh?)

// AppController.php
public function beforeRender(){
// When setting _serialize, you pass in a list of variable names.
// array_keys($this->viewVars) returns an array of the names of the view variables 
$this->set('_serialize', array_keys($this->viewVars));
}

That works, but if I need a way to ignore some view variables, I would have to do something a little more elaborate.

// AppController.php
public function beforeRender(){
// When setting _serialize, you pass in a list of variable names.
// array_keys($this->viewVars) returns an array of the names of the view variables
$viewVars = array_keys($this->viewVars);
 
// I include _serialize to prevent the possibility of a _serialize inside another _serialize 
$varsToIgnore = array('_serialize', 'debugToolbarPanels', 'varThatOnlyAppliesToBackEnd');
 
foreach($varsToIgnore as $varToIgnore){
// Might be a better way of doing this, but it works and seems safe
// in_array checks if a value exists in an array
// unset removes an array or array index in this case
// array_search returns the index of where a value is found 
if(in_array($varToIgnore, $viewVars)) unset($viewVars[array_search($varToIgnore, $viewVars)]);
}
 
// Reindex the viewVars array after removing variables from above
// I don't know if this is really necessary, but I did it anyway.
$viewVars = array_values($viewVars);
 
$this->set('_serialize', $viewVars);
}

My beforeRender method is getting a little too long for my liking, so I decided to clean it up a bit, by moving the variables to ignore into a private property and the logic to serialize the view variables into its own private method.

// AppController.php
// A shorter name would be easier to type
private var $viewVariablesNotToSerialize = array('_serialize', 'debugToolbarPanels', 'varThatOnlyAppliesToBackEnd');

public function beforeRender(){
$this->__serializeViewVars();
}

private function __serializeViewVars(){
// When setting _serialize, you pass in a list of variable names.
// array_keys($this->viewVars) returns an array of the names of the view variables
$viewVars = array_keys($this->viewVars);
 
foreach($this->__viewVarsNotToSerialize as $varToIgnore){
// Might be a better way of doing this, but it works and seems safe
// in_array checks if a value exists in an array
// unset removes an array or array index in this case
// array_search returns the index of where a value is found 
if(in_array($varToIgnore, $viewVars)) unset($viewVars[array_search($varToIgnore, $viewVars)]);
}
 
// Reindex the viewVars array after remove variables from above
// I don't know if this is really necessary, but I did it anyway. 
$viewVars = array_values($viewVars);
 
$this->set('_serialize', $viewVars); 
}

I hope someone finds this useful.

Matt Bowden
Web Application Developer
Skybrick

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.

Re: Defining Plugin

Yes, I guess you have been creating non-conventional links so far. Mainly because you are confusing framework internal naming schemes with URL naming scheme
The latter defines everything underscore_lowercased, even the plugin.
The first refers mainly to classes, which will always be named CamelCased.

You need to understand the difference in order to not write faulty code - which at some point can blow up (as it then did).


Am Samstag, 26. Oktober 2013 04:43:11 UTC+2 schrieb CrotchFrog:
I've noticed recently some strange behavior when it comes to naming conventions and Plugins.  

For example if I create a link somewhere within a plugin ie. $this->Html->link('Some Link', array('controller' => 'controller', 'action' => 'action')); 
Following the link would land you on the page app/plugin/controller/action unless a location is specified outside of the current plugin and controller. 

Links created this way and that have been working since they were created sometimes out of the blue throw an error along the lines of SomePluginController cannot be found. 
This is strange because the link has always worked in the past. When these links create an error I have to re-create the link and specify the plugin $this->Html->link('controller' => 'controller', 'action' => 'action', 'plugin' => 'some_plugin')
I have to use an underscore in the plugin name, using camel case results in the same error. 

It's possible that some, but not all, of the links were created only specifying the action and not the controller. With this being the case I could sort of understand the resulting error but not when I have specified both the controller and the action.

I created all of my custom routes with the plugin as camel case, it just seems to be links and redirects that display this behavior.  

I assumed that with sticking to naming conventions I could write it as SomePlugin or some_plugin and either one would be fine. 

Have I been creating my links incorrectly this entire time? 

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.

Re: Defining Plugin

I've never used pagination for a plugin in before, but I'm guessing specifying the plugin for the model would be required. i.e. $this->paginator->sort('username', 'Username', array('model'=>'SomePlugin.Model'));

Though I guess with doing that, there would have to be an assumption that the model and the controller are in the same plugin.

There's also a url option in which you can specify the actual url to use, if using the model does not work.

Regards
Reuben Helms

On Monday, 28 October 2013 05:49:51 UTC+10, CrotchFrog wrote:
I did find one place so far that this problem with plugin names is creating  an issue whereas I feel that it should not be.

Working with a table (in a plugin) I define a heading as $this->paginator->sort('username', 'Username'); which in turn creates the link as <a href="/admin/UserManager/warnings/index/sort:username/direction:asc">Username</a> which seems just fine from all outward appearances, the only problem is that instead of sorting the data as expected I'm greeted with the all-too-familiar error UserManagerController could not be found. 


On Friday, October 25, 2013 10:43:11 PM UTC-4, CrotchFrog wrote:
I've noticed recently some strange behavior when it comes to naming conventions and Plugins.  

For example if I create a link somewhere within a plugin ie. $this->Html->link('Some Link', array('controller' => 'controller', 'action' => 'action')); 
Following the link would land you on the page app/plugin/controller/action unless a location is specified outside of the current plugin and controller. 

Links created this way and that have been working since they were created sometimes out of the blue throw an error along the lines of SomePluginController cannot be found. 
This is strange because the link has always worked in the past. When these links create an error I have to re-create the link and specify the plugin $this->Html->link('controller' => 'controller', 'action' => 'action', 'plugin' => 'some_plugin')
I have to use an underscore in the plugin name, using camel case results in the same error. 

It's possible that some, but not all, of the links were created only specifying the action and not the controller. With this being the case I could sort of understand the resulting error but not when I have specified both the controller and the action.

I created all of my custom routes with the plugin as camel case, it just seems to be links and redirects that display this behavior.  

I assumed that with sticking to naming conventions I could write it as SomePlugin or some_plugin and either one would be fine. 

Have I been creating my links incorrectly this entire time? 

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.

Re: Defining Plugin

I did find one place so far that this problem with plugin names is creating  an issue whereas I feel that it should not be.

Working with a table (in a plugin) I define a heading as $this->paginator->sort('username', 'Username'); which in turn creates the link as <a href="/admin/UserManager/warnings/index/sort:username/direction:asc">Username</a> which seems just fine from all outward appearances, the only problem is that instead of sorting the data as expected I'm greeted with the all-too-familiar error UserManagerController could not be found. 


On Friday, October 25, 2013 10:43:11 PM UTC-4, CrotchFrog wrote:
I've noticed recently some strange behavior when it comes to naming conventions and Plugins.  

For example if I create a link somewhere within a plugin ie. $this->Html->link('Some Link', array('controller' => 'controller', 'action' => 'action')); 
Following the link would land you on the page app/plugin/controller/action unless a location is specified outside of the current plugin and controller. 

Links created this way and that have been working since they were created sometimes out of the blue throw an error along the lines of SomePluginController cannot be found. 
This is strange because the link has always worked in the past. When these links create an error I have to re-create the link and specify the plugin $this->Html->link('controller' => 'controller', 'action' => 'action', 'plugin' => 'some_plugin')
I have to use an underscore in the plugin name, using camel case results in the same error. 

It's possible that some, but not all, of the links were created only specifying the action and not the controller. With this being the case I could sort of understand the resulting error but not when I have specified both the controller and the action.

I created all of my custom routes with the plugin as camel case, it just seems to be links and redirects that display this behavior.  

I assumed that with sticking to naming conventions I could write it as SomePlugin or some_plugin and either one would be fine. 

Have I been creating my links incorrectly this entire time? 

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.