I am using cakePHP v1.26.
In the default.ctp file,
I got a single of this code in it:
$session->flash();
I came a corss a web site in which the author suggested using this instead:
if($session->check('Message.flash')){
$session->flash();
}
I do not understand what this line of code is doing:
if($session->check('Message.flash')){...}
what is "Message.flash" in this case?
Is "Message.flash" a custom variable or
a built-in varibale which has been predefined in cakePHP?
Message.flash is the session variable name. It will be defined by cakephp, when you use $this->Session->setFlash('Your message'); from your controller.
if($session->check('Message.flash')){...} checks, if session Message.flash, which contains the flash message, exists.
Note also that contrary to the current manual description, $session->flash() does not echo the result, it just returns it, so you will need to have
echo $session->flash();
in your view.
For latest cakephp version
if(!($this->Session->check('Message.flash')));
// your code
In view section for show messages.
$this->Session->flash();
Related
I'm using CakePHP (v3.0) and I want to show on screen the variable value.
I have tried to use print_r() function, but not always runs..
Whats would be the best option?
CakePHP comes with pr() function you can use that
Documentation link
I always use cake's debug() function, but you should consider using DebugKit for debugging your app.
DebugKit
I'm new in CakePhp but experienced in CodeIgniter. I created a controller in "WelcomeController.php" in controller directory and run the page. I got two errors
1. Error: The view for WelcomeController::index() was not found.
2. Error: Confirm you have created the file: C:\xampp\htdocs\myc\app\View\Welcome\index.ctp.
My question
Why I am getting this error even though I have supplied index() function?
In codeigniter we may not create a directory for a view. I don't want to create a directory "Welcome" in view. I there any provision provided?
In Cakephp you have to create view for function or here it called action. In your case, Create index.ctp on App->View->Welcome folder. This Getting Start
will give you a basic idea.
1) You're getting that error because your missing the view, not the controller function. To fix, do what the error suggested:
Confirm you have created the file: C:\xampp\htdocs\myc\app\View\Welcome\index.ctp.
2) "I don't want to create a directory "Welcome" in view. I there any provision provided?".
Not really... I mean, no if you want that action to have a correspondent view to put the content. Otherwise you can use $this->autoRender = false to not show anything... But that'll mean the url localhost/welcomes/index will be blank.
I recommend you read the basics as Fazal said. I know every framework can give us "quirks" and we end up expecting every other framework to work the same way we are used to, but try to adapt to the cake-way.
Btw, should be "WelcomesController", according to cake conventions
Be sure that you have all the files and directories necessary for the modal, controller and view to link up correctly i.e.: Create the folder called welcome(s) in your views directory with an index.ctp file. This should get rid of that error.
Check out this brilliant blog tutorial: Link
I am new to cakephp and I think I've run into my first problem because I need 2 $content_for_layout in my default layout, like this:
<div id="left_collumn">
<?php echo $content1_for_layout ?>
</div>
<div id="right_column">
<?php echo $content2_for_layout ?>
</div>
I've searched this group and found a thread that suggest using this : http://bakery.cakephp.org/articles/rtconner/2007/08/28/anything_for_layout-making-html-from-the-view-available-to-the-layout and even though I followed the instructions for some reason this is what I always get :http://img38.imageshack.us/img38/3246/sshot1duh.jpg
Do you have any idea why this is happening?
I am not sure what you are trying to do, but maybe you could use an element instead...
http://book.cakephp.org/view/1081/Elements
Read about plugins, thats not the way! Lear to make your own plugin!
Yeah you should use elements instead. Try to go with the rules of the framework whenever you are working on it. There is no need to change the way how Cake PHP handles it, rather using the solutions which framework offers. Use Elements.
http://book.cakephp.org/view/1081/Elements
I have been looking into this for a while and can't figure it out. Basically I have an add page for my model which you can add a map from a URL or from a file upload. I have got all the fields and validation in but how and where do I manage the uploaded file?? There must be some easy way to do this. Thanks!
Firstly your form needs to be set up to allow file uploads.
<?php echo $form->create(Model, array('type' => 'file')); ?>
This will allow any file inputs to actually upload the file to your server with $form->file(field) or $form->input(field, array('type' => 'file')).
Once the file has been uploaded you should handle everything else from within the Model:
function beforeSave($created) {
extract($this->data[Model][field]);
if ($size && !$error) {
move_uploaded_file($tmp_name, destination);
$this->data[Model][field] = destination;
}
return true;
}
These are only the basics, so be sure to have a play around to find the solution that best fits your needs.
You can use Zend Components to handle the file upload. There is a good example here on my website:
CakePHP file upload using Zend Components
NOTE: MeioUploadBehavior has been deprecated. Instead jrbasso suggests the Upload Plugin.
In addition to the fine answers already given I want to hint about MeioUploadBehavior, currently maintained by jrbasso at github, which has been a great help for me in my own CakePHP project.
You simply add the behavior to your model using the $actsAs field and at the same time specifying any custom preferences. Then create necessary fields (described by supplied docs in detail) in your database, or configure the model to not use any database table. Finally setup the form in your add page, also described in the supplied documentation. The behavior will then take care of the rest for you.
When using the paginator helper in cakephp views, it doesnt remember parts of the url that are custom for my useage.
For example:
http://example.org/users/index/moderators/page:2/sort:name/dir:asc
here moderators is a parameter that helps me filter by that type. But pressing a paginator link will not include this link.
The secret is adding this line to your view:
$paginator->options(array('url'=>$this->passedArgs));
(I created this question and answer because it is a much asked question and I keep having to dig out the answer since i cant remember it.)
To add to Alexander Morland's answer above, it's worth remembering that the syntax has changed in CakePHP 1.3 and is now:
$this->Paginator->options(array('url' => $this->passedArgs));
This is described further in the pagination in views section of the CakePHP book.
$this->passedArgs is the preferred way to do this from the view.
You saved me! This helped me a lot, Thanks.
I needed a way to pass the parameters I originally sent via post ($this->data) to the paging component, so my custom query would continue to use them.
Here is what I did:
on my view I put
$paginator->options(array('url'=>$this->data['Transaction']));
before the $paginator->prev('<< Previous ' stuff.
Doing this made the next link on the paginator like "
.../page:1/start_date:2000-01-01%2000:00:00/end_date:3000-01-01%2023:59:59/payments_recieved:1"
Then on my controller I just had to get the parameters and put them in the $this->data so my function would continue as usual:
foreach($this->params['named'] as $k=>$v)
{
/*
* set data as is normally expected
*/
$this->data['Transaction'][$k] = $v;
}
And that's it. Paging works with my custom query. :)
The options here are a good lead ... You can also check for more info on cakePHP pagination at cakephp.org/view/166/Pagination-in-Views
With that param 'url' you can only put your preferred string before the string pagination in url..
if I use this tecnique:
$urlpagin = '?my_get1=1&my_get2=2';
$paginator->options = array('url'=>$urlpagin);
I only obtain:
url/controller/action/?my_get1=1&my_get2=2/sort:.../...
and Cake lost my get params
Have you an alternative tecnique?