Creating a view (cakePHP noob) - cakephp

I've been reading until my eyes are swollen, and having troubles finding what should be a simple answer.
I am not new to PHP, but I am new to cakePHP. Please bear with me, and have patience with my ignorance of lack of knowledge of the terminology.
I have been asked to help make some fixes on a cakePHP developed site, that was recently created.
The site is missing a page for "http://domain.com/logout". I can see the functions I need to access in the UserController, but I am not sure where to put the .ctp file to generate the view.
Let's just say I want the logout.ctl file to be something as simple as:
echo "Hello World";
Under my app/View folder I have the sub-folders Home, & User that I have tried to place this file into. I am assuming I have to do something else as well, but I have not been able to locate what that is.
Any assistance is appreciated. Thanks for reading!

1.By default, you should link your view and controller together by creating Views/Controller/action.ctp.
Since the url is linked to Controller by routes, views are not associated with it directly.
For example, if you have set
Router::connect('/logout/', array('controller' => 'User', 'action' => 'logout'));
, then you may want to create Views/User/logout.ctp.
If you have set
Router::connect('/logout/', array('controller' => 'Home', 'action' => 'logout'));
, then you may want to create Views/Home/logout.ctp.
2.You can change the view in your action with $this->view='sample' or $this->render('sample'); and then create the view file with name sample.ctp.
3.You can also read a view of another folder with $this->render('/Sample/logout');.
Reference: http://book.cakephp.org/2.0/en/controllers.html
4.If you use themes $this->theme = 'Example';, the default view file will be set to /app/View/Themed/Example/Posts/edit.ctp.
Reference: http://book.cakephp.org/2.0/en/views/themes.html
5.I think the default extension of cakephp view files is .ctp but not .ctl. .ctl is used by Microsoft Visual Studio? I'm not quite sure.

Related

Extend RedisEngine in CakePHP

I have some custom methods created in Cake's RedisEngine file to do things like Redis spop...etc. I know that editing the actual RedisEngine file itself within the Cake Lib is not ideal, but am unsure exactly how to extend it so that I can add my own methods. Or more specifically, if I do extend it, how to I tell Cake to use MyRedisEngine instead of the default one?
Per this page in the CakePHP Book, you can extend CacheEngine by specifying the config like this in Config/bootstrap.php:
Cache::config('custom', array(
'engine' => 'MyCustomCacheEngine', // if this doesn't work, try without the 'Engine'
// ...
));
and adding a MyCustomCacheEngine.php file in the app/Lib/Cache/Engine/ directory.

Should I create a new controller in cakephp

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 don't get chosen-cakephp it working in simple application

I am new to CakePHP, MVC and web developing in general (JQuery, etc), altough I have more than 15 years programming non web applications and some little knowledge of PHP.
In order to learn, I am developing a simple school project containing 3 tables (courses, professors, courses_professors).
I have "baked all" and got a nice CRUD application which works perfectly.
Now I am trying to improve select boxes userfriendliness provided by harvesthq/chosen by using Chosen-CakePHP.
I have followed the instructions for installation and setup of the plugin at https://github.com/paulredmond/chosen-cakephp, but I am not sure about what more steps should I follow in order to make it work. Btw. I understand creating a custom class is optional.
To be true, I expected that modifing /app/Controller/AppController.php as explained in would be enough to see the new select boxes working, but the application works just as it did before adding the plugin.
The code contained in /app/Controller/AppController.php is the following (comments removed):
App::uses('Controller', 'Controller');
class AppController extends Controller {
public $helpers = array('Chosen.Chosen');
}
app/Config/bootstrap.php contains:
./...
CakePlugin::load('DebugKit');
CakePlugin::load('Chosen');
./...
I have been googling for 2 days, but I don't get the idea.
Do I need to modify any code generated by bake? Do I need to modify anyother files?
I guess my lack of knowledge in the field is quite relevant. Should you reccomend me an essential previous reading or exercise, any reccomendation is welcome.
Any indications would be appreciated.
Thanks for your help,
ivan
I have never work with the plugin you mention, but look at the examples in the link you provided
(in the view)
echo $this->Chosen->select(
'Article.category_id',
array(1 => 'Category 1', 2 => 'Category 2'),
array('data-placeholder' => 'Pick categories...', 'multiple' => true)
);
The helper used is not $this->Form->input, as it comes with baking, it's $this->Chosen->select. So my guess is "yes, you need to modify the code generated by bake".
Try that, and if it doesn't work, update your question with the code you tried for the view and if it gives you any (new) error.
You don't really need a plugin to get such a small functionality for your forms.
First you need to download a Chosen package.
Open your webroot folder and put there all dependent js, css files that comes with package
Open default layout file app/Views/Layouts/default.php (if you "baked all" that should be default layout file)
Read about inserting js, and css files into your layout here Html Helper
Syntax is simple:
This code:
echo $this->Html->css('forms');
Outputs:
<link rel="stylesheet" type="text/css" href="/css/forms.css" />
Similar for js files..
Add this code to layout footer
$(".chzn-select").chosen();
or header (but then you need to wrap it with document.ready ;
In your baked view files add class .chzn-select to select form elements
echo $this->Form->input('Model.field', array(
'type' => 'select',
'multiple' => true,
'class' => 'chzn-select'
));
If you just started learning, it's to early to just "bake all"

How to add a cake php file in another cake php file

I am using cake php now. I have two file example1.ctp and example2.ctp. I want to include example2.ctp in example1.ctp just like how we add php page using "include". I am new to this please suggest me how to do it.
According to me , You have to create Elements..
CakePHP can help you repeat parts of your website that need to be reused. These reusable parts are called Elements.
<?php echo $this->element('ur elements ctp file name'); ?>

How to do form-based file uploads in CakePHP?

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.

Resources