I understood (more or less) the separation between the MVC parts in cakePhp, however i cannot understand what are the defaults.
meaning:
What should i edit in order to change the root-entry-point of my site(the known "index.html" or "index.php" file, that shouldn't be changed in cake)?
What controller? What model? What view? What layout?
(hope I'm understood)
(i am using version 1.3)
thanks
What you should edit to modify the root is:
app\views\pages\home.ctp
The default layout can be tweaked here:
app\views\layouts\default.ctp
From there on you can create your menus, links etc to other controllers of other pages, then involving the traditional MVC patterns/conventions you already know.
Addition:
If you want to provide a link to your statistics then use for example:
echo $html->link('My nice statistics',
array('controller' => 'statistics', 'action' => 'show'));
If you would like to embed the statistics then I would use elements: http://book.cakephp.org/view/1081/Elements.
I am not sure about what you missed: maybe the fact you can specify the controller to use for links if it is an external controller to the MVC scope currently used.
Are you sure you have understood the conventions behind MVC? Here is the tutorial I started with some time ago. It is well made but a bit out of date for cakephp 1.3. Nevertheless it illustrates the basic concepts very nicely: Cook web sites fast using CakePHP (IBM)
I hope this is more helpful then :-)
The default Route in Cake routes the address / to the PagesController::display action with the parameter "home", which will make the Pages controller display the file /views/pages/home.ctp. If you just want a static home page, just edit that file.
If your default homepage at / should display more complex data, including model data, you would rather create your own controller with a model and its own directory in /views/ and change the default route for / in /app/config/routes.php to point to an action of that controller.
Related
I did not found any answer to this. So is there a way in Wagtail to have an AbstractEmailForm without AbstractFormField (for example, can I hard code them into AbstractEmailForm? I know that AbstractEmailField has some variables that Django requires). I just need to have a contact form only with email field, I dont need to set fields dynamically.
And the second question: How do I set permission for that form page so that only superuser can edit the form page?(And the translated version too? There are a lot of answers to this, but I don't actually understand how to do it with AbstractEmailForm).
Thanks a lot!
If you're hard-coding your form fields, then the Wagtail forms module doesn't really give you anything that you don't already get from Django's forms framework, and so you're better off using Django forms directly. The serve method on a Wagtail page is equivalent to a Django view function, so any form-processing logic that would normally go into a view function can be placed in serve.
There's an example of this here (but written for Wagtail 1.x, so imports will need adjusting): https://github.com/gasman/wagtail-form-example/commits/master
I want to call a .ctp file in one 'Model' and the .ctp file is in another 'View'.
Is it possible in cakephp?
Or instead of that should i call that 'Controller' function in my 'Model'?
CakePHP in a nutshell, and the keywords that you might need to search for:
Dispatcher and Routing controlling how URL reaches your controller.
Controller places system logic and controlling individual routing requests from Dispatcher
Component places logics that can be easily shared by Controllers
Model is for all the database related queries, manipulation, selections, deletions
Behavior can be deem as similar to Component, that provides "mixins" to Models to achieve similar behaviors, such as TreeBehavior abstract your database tables into parent-child relationship.
View is used by Controller to render individual pages to the user
Helper placed shared functionalities to help View render certain stuff. For example, FormHelper helps you all sort of form rendering, inputs, etc.
Place globally shared library in app/Lib folder so it can be easily accessed through using App::uses('...', 'Lib'). For example, a Gravatar library that helps you convert emails to md5-hashed strings. So this can be used everywhere in your app.
vendors are for those packaged vendor libraries that do not respect MVC, for example, swiftmailer that helps you send emails. Usually I would abstract them into my Lib folder for ease.
plugins are for those baked CakePHP applications found everywhere in the internet.
There are others in-depth stuff that you might be interested in, but these are the most basic stuff that you need to know before using a MVC framework like CakePHP. Check out their docs before diving in.
You can't access View (.ctp) in Model, it's against MVC architecture and logic. Just tell us more what do you want to do, maybe you're doing something wrong.
I am trying to move from CodeIgniter to CakePHP and can't figure out the best way to implement an admin panel. In CI I would create two different applications, one for the frontend and one for the admin panel.
After Googling around, I have found three ways to implement admin panel in CakePHP:
Routing - I don't want to use this as I want by Controllers/Models to be separate for frontend and admin panel
Plugin
Two separate apps
Should I use plugin to implement admin panel or should I have separate apps? Any benefits of one over the other?
I normally develop the admin/backend as a plugin. This keeps your backend/admin controllers/views/models separated from the frontend and you don't have to jump through hoops to have separate stylesheets, layouts etc.
Another advantage is that both front- and backend are still part of the same application, so if desired, you can share logic/components, for example you'll be able to put helpers that are usable both for front- and backend in another plugin (e.g. plugins/Shared or plugins/Handytexttools) and use those both wherever you want
As a rule of thumb; put components that may be reuseable for other projects in a separate plugin, this way you can just add those plugins to other projects without problems. Keep your plugins simple; it's no problem to create a plugin containing just one or two helpers or models and a few files of JavaScript. This will make it easier to 'cherry pick' the plugins that you need for a project. Once Cake has 'cached' the file-locations of all classes in your plugins, the overhead of separate plugins should be minimal.
Coming back to the 'admin' plugin. Try to only include code specific for this project in your admin plugin and reusable parts in another one (e.g. Generic stylesheets and layouts for admin-panels). You'll be able to start a admin-plugin for your next project with minimal coding
Good luck with your project and enjoy CakePHP
If you want to keep your controllers and models separate - I'd go with a separate app, although you'll end up with a bunch of duplicate code between the apps (maintenance headache waiting to happen).
My choice would be admin routing and an admin theme.
Enable admin routing in /app/Config/core.php
In AppController beforeFilter():
$this->theme = isset($this->params['admin']) ? "Admin" : "Site";
Move all your site views and assets into /app/View/Themed/Site/
Create your admin themes in /app/View/Themed/Admin
Old and refers to CakePHP 1.3, but still is a question you should check: CakePHP admin panel
The Cake way is routing. I'd go with a plugin like CakeDC Users that makes things easier.
You could use admin-routing. Check out:
http://book.cakephp.org/2.0/en/development/routing.html#prefix-routing
Another solution -which I find really easy to implement- is like this:
In your AppController:
public function beforeFilter(){
$this->set('current_user', $this->Auth->user());
}
This makes the $current_user available in your app.
Then in your view-files, you can check:
<?php if ($current_user['role'] == 'admin'){/*place code for admin users to see here*/} ?>
<?php if ($current_user){/*place code for logged-in users to see here*/} ?>
I know this is an old thread. But would like to ask if anyone had trouble implementing the admin panel as a plugin. Particularly duplication of code.
For example you're implementing an e-commerce site. You have an OrderController both in the main and admin plugin. Don't you think it's kinda hard to maintain the logic in two places?
How about just using one main controller. It's serves two purpose. One as an API then the controller for your Admin webapp.
Your public side would then basically communicate via API to fetch data.
Do you think it's a good idea?
You can use admin views like admin_index.ctp just change this
//Configure::write('Routing.admin', 'admin');
to
Configure::write('Routing.admin', 'admin');
in core.php and in the controller add admin_index() function
What's a good why to use CakePHP 1.3 for my website, but I have no database, since the site is pretty static?
You can use simple HTML to develop a site which is having all Static Content. And if it having a single contct page or something which you can develop in simple PHP. You don't need to use CakePHP for that.
CakePHP framework is used for web application that needs to interact more with databases.
Although I generally agree with gvLearner's point if you'd like to have a static html site, but still use some of CakePHP's abilities and be able to easily transform the site to a dynamic one in the future you can use Cake's built in Pages Controller.
The CakePHP core ships with a default controller called the Pages Controller that is generally used to serve static pages. Check out the documentation link I sent, I hope it'd would do you good.
Cheers,
Borislav.
I am new to Cake PHP, in regular PHP I had a index.php and for any bad url tried it would just show home. In Cake, I am switching an old PHP and HTML website to use Cake PHP. I already successfully converted one page :(. Which is good but that page is just about_us. I have not done Home (which would be index.php). So here is my scenario
I created all the controllers and models with no code in it except for this one page that gets a bunch of products,but I have the following questions as I do not understand CakePHP documentation:
1) How can I set up an index.php page and where should I put it and Actually this page needs to grab something from the db too. Should it go under views/pages? (i am not sure)
2) Also how do i retrieve parameters in Cake PHP? i used to have index.php?name=blah and just pull name in. I am not too clear on how the cake website says to do it.. you just add the parameter after a /index/2 for example? How come?.
Thank you
I think cake is quite well-documented. You just didn't take enough time to read the book.
1) If you want to create a 'Home' page, it doesn't have to be an 'index.php' file.
(cake has some index.php file to call the bootstrap and dispatch logic and don't mess with them)
You can
- create a view (some .ctp file), put it to /app/View/Pages/ so you can use the url '/pages/' to access the page. Or you can edit /app/Config/route.php file to connect the page to whatever path you want
- create a normal controller/action/view (if you need to grab something from the db so you should have some models, your controller will call the models and pass data to the view). You can edit /app/Config/route.php to connect the page with your path.
Cake is convention-over-configuration. But you should understand both the convention and the configuration. Read the book. Learn more about MVC, about cake's convention and mechanism.
2) You can retrieve the parameter from controller or view (you should do it in the controller) by using $this->param or $this->passedArgs. There are named ones (e.g: profile/name:john) and unnamed ones (e.g: profile/john).