cakePHP project, orgazization of folders, pages, functions - cakephp

Im new to cakePHP, and Im wondering how a 'live' site does this.
I see 2 possibilities :
1) There is one controller with a bunch of pages (functions) in its (extended) AppController.
2) There are many controllers, each with a small number of pages (functions) in its (extended) AppController.
(You probably get my question already, but Im going to say it in another way too)
Should I put my contact page in a separate controller than my blog page? (I have a hunch the answer is yes.) Why?

You don't need to create a controller for everything. In fact, you shouldn't, because there are better ways around it. The more static pages you have, the more out of hand it can get.
For Static Pages
Copy pages_controller.php from the cake/libs/controller folder over to your app/controllers folder. Add the following piece of code to your display() action:
function display() {
...
$page = Inflector::slug($page);
if (method_exists($this, $page)) {
$this->$page();
}
$this->render(join('/', $path));
return;
}
Then, modify your routes.php file to add the various static pages:
Router::connect('/about', array('controller' => 'pages', 'action' => 'display', 'about'));
Router::connect('/contact', array('controller' => 'pages', 'action' => 'display', 'contact'));
Now, the contact form is a static page, but has some logic attached to it. So, you can head over to your PagesController and create an action specifically for this (or any other page that isn't merely static):
function contact() {
if (!empty($this->data)) {
...
}
}
Basically, the route directs your static page request to the PagesController's display() action. The display action checks if a method of the same name exists. If it does, it executes that action and displays the pages/{page}.ctp view.
For Non-Static Pages, eg. Blog
Now, this definitely needs a model. In fact, multiple models (Post hasMany Comment, Post HABTM Tag). In order to manipulate and access these different models, it's better that you place the code into a separate controller.
A lot of people like to name their controllers based on their URLs. For example, they name their controller as BlogController if they want a URL such as /blog.
A method that I prefer is using routing to get the URLs that I want, and keeping controllers named as per CakePHP conventions.
Eg. A PostsController would control the Post model and related models. But if I wanted the /blog URL to display a list of all the posts, I would write a route to point it to /posts/index.
Router::connect('/blog', array('controller' => 'posts', 'action' => 'index'));
You can have additional routes too. Example: /blog/2010/06/10/whats-in-a-post to point to /posts/view/.
Again, this is just an example of what's possible. In the end, you should stick to the methods that you think helps keep your code organized for both you and your team.

Related

CakePHP: Flexible redirection for views and controllers

I have views that are called from different views. The customers edit view can be called from the customers list, the customers search, from within an order and so on. Some of this views are simple views other contains forms to add, edit or delete data. After the user has done what he had to to on that form he should be redirected to the calling form or to another form.
Using $this->referer() wouldn't work as some navigations have to go like this:
list order --> edit order --> delete order --> list order.
I would be fine with defining the redirection for every call so I've tried to use query strings and add ?redirectTarget=<wherever> to every link or redirection. For that I've made a controller function
in AppController.php
public function getRedirectTarget() {
if ($this->request->query('redirectTarget')) {
return $this->request->query('redirectTarget');
} else {
return array(
'controller' => 'pages',
'action' => 'home'
);
}
}
This works for forms as I can use getRedirectTarget() in my controllers but I cannot access that function from within a view to build a link. (At leas I sholdn't do that Can you call a controller function from a view in CakePHP? ) In the example from the top I have to pass the information from the order list to the edit view to build a link and to the underlying controller for the form action.
Now I have different aproaches in my mind but with none of them I'm realy happy. I'm not shure which way to go.
Is there something in CakePhp I haven't found yet?
using $this->requestAction?
changing everything to forms and buttons and doing all redirections in the controllers?
Is there a way to create a variable for every view?
As this seems to me like a comon requirement and I'm pretty new to CakePHP I'm asking for your advice.
Using requestAction should be the best way.
In the views, you can have something like
$redirect = $this->requestAction('/mycontroller/requestAction');
$url = $this->Html->link('Continue', $redirect));

CakePHP Routing & HTML Helper Link Ambiguity

I have a site that uses CakePHP 2.x. There's a backend interface where actions use the standard Cake layouts and views, but several of the actions are also exposed to front end users as "dialogs" (same functionality, just a layout that can be put in iframe).
In app/Config/router.php I have added the following:
Router::connect('/dialog/:controller', array('action' => 'index'));
Router::connect('/dialog/:controller/:action');
Router::connect('/dialog/:controller/:action/**');
This works appropriately, but the problem starts when trying to use the HTML helper's link() method. If I try to create a link like:
$this->Html->link('edit account', array('controller' => 'users', 'action' => 'edit'));
I get the following:
edit account
When the link is within a dialog, this works great, but I don't want the non-dialog pages to link to the dialog.
How can I control which of the two URLs is used in a particular page?
Is there something I can call from within AppController once I know whether the page being rendered is a dialog or not, or even something in the call to link() that would allow me to override it.
I know there's the "prefix" option which would allow for URLs like /user/dialog_edit but I would like to maintain the /dialog/users/edit format if possible. I also know I can hard code the URL vs. passing controller/action/id/etc in an array, and I don't anticipate pathing/model names changing, but I'd like to do this the idiomatic way for CakePHP, if possible.

How can I deny test.ctp and allow home.ctp to anonymous users in CakePHP?

I'm using AuthComponent in CakePHP 2.
I want to show to anonymous users just the 'home' view and deny 'test' view.
Both views are inside the folder Views/Pages
If I use this code:
public function beforeFilter(){
parent::beforeFilter(); //calling parents before filter
$this->Auth->allow('home'); //allowing home only for pages controller
}
in my PagesController I'cant access any page of my PagesController, including home.ctp.
If I use:
$this->Auth->allow('display')
in my PagesController I can access any page of my PagesController, including test.ctp.
How can I solve this issue?
I tried to create a new method on my PagesController name home(). But without success.
Here is a good answer for you.
Allowing a Specific Page in Cakephp
Keep in mind that the Auth->allow is for methods. Display is the method in the pages controller. The Home page is considered a variable used in display. So you will have to do this programmatically in the Pages controller.
Situation one: If you dont want to utilize the default 'display' function of CakePHP.
Along with creating a new method in PagesController named home(),
you must also alter the code in the file app\Config\routes.php as shown below to make it work for home and not for other methods, this new code redirects all queries to pages controller to home.ctp.
//default code statement:
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
//New code
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'home'));
Situation two: If you want to keep the default 'display' function of CakePHP
Then you must build some redirect logic in display function when visitor is accessing test.ctp page, as also answered by #Chuck Burgess with a link.

dynamic html redirects using cakephp routers

dynamic html redirects  using routers
in the beginning I had no categories and all of my pages were root
example:
http://domain/somepage
This was great, but as my content over the years grew I need to categorize my content
so I added some routes see below
//routes.php
Router::connect(
"/:category/:slug",
array('controller' => 'controllername', 'action' => 'view'),
array(
'name'=>'[-A-Z0-9]+',
'pass' => array('category','slug')
)
);
//end
this works great and accomplished what I needed to do, but there is one problem the search engines .I need to write 301's for all of my links and I have over 8K pages.
The solution cakesphp's Router::redirect
The issue I am now having is I cant figurer out how to redirect my old links. I can for example redirect all of the links to one category, but that wont cut it. I need to redirect all of my links to the new location.
I am trying to use routes.php router :: redirect
if I do this my code it redirects to the category, but not the slug
Router::redirect(
'/:slug/*',
array(
'pass' => array('category/:slug'))
result
http://domain/category/
how can I get cake to redirect to
http://domain/category/slug ?
instead of http://domain/category/
I had all of my links pointing to the root directory
http://domain/somepage
http://domain/anotherpage
http://domain/ect
I needed to add categories
such as
`
http://domain.com/phones/samsung.php
http://domain.com/books/cakephp.php
`
I didn’t want to use .htaccees file because
My hosing provide limits me to 100 redirects
and i have over 8K links i need to redirect
I am trying to use cakes router ::redirect function in the routes.php file.
the below code works only for one category it doesn’t do it dynamically like I would like it too.
I tried to create a router class that would do this for me like you suggested, but to be honest with you I am not an expert in cakephp. Its easy to learn and a great framework I just dont know how to make my own classes or components yet. I just haven’t found good documentation to do this yet.
//routes.php
$move='category/'. stripslashes_deep ($_SERVER['REQUEST_URI']); Router::redirect('/:slug/*',$move, array('status' => 302)); code
Your best bet is to use a custom route class.
Custom routing extends the CakeRoute class and will allow you to return/add/modify parameters that are passed over.
CakePHP Custom Route Classes - How to Pass Arguments?

adding a view just with static data

I have a cake php application and 4 or 5 pages contain static pages in .php, I have also built some controllers/models and views for those pages that need to be dynamic. My question is.. Can I have a view just with static code there and call it something like
http://myweb.com/app/someemptycontroller/staticview/
Can this be done and if so how do i need to set it up in terms of model and controller..(maybe empty) i am not sure as I am a newbie in this cake php world.
Thank you
CakePHP comes with a working pages controller, which can be used to serve up static pages.
Any view you place in /app/views/pages can be accessed like (say your view is hello.ctp):
http://myweb.com/pages/hello
If you don't like the pages controller showing in the URL, add a line like this to your app/config/routes.php file:
Router::connect('/hello', array('controller' => 'pages', 'action' => 'display','hello'));
Now your page can be accessed by:
http://myweb.com/hello

Resources