This idea seemed to work out. Basically what I needed to happen was to
allow access to multiple controllers for data objects associated with
a given page in the CMS. The data objects themselves are included in
the page through a modified request action (the version by biesbjerg).
So all I needed was to somehow set things up so that Cake knew which
page we were on. Luckily I could access a custom class from within
routes.php and use that to generate the base URLs and connect them to
the proper controller/id combinations (this always uses the same
action). As a bonus I can save these URLs in a singleton class so I
don't need to query the DB again to figure out how to build links in
the views (which did require a custom helper).
The rough skeleton of what I did is in the following function:
function pageRoutes(){
$Page = new Page();
// get the tree list
$treelist = $Page->Behaviors->Tree->generatetreelist($Page,
NULL,
'{n}.Page.id', '{n}.Page.alias', '/', 'Page.site_id,Page.lft ASC' );
// get all pages in the tree
$pages = $Page->find(
'all',
array(
'conditions' => 'Page.id
IN('.join(',',array_keys($treelist)).')',
'fields' =>
'Page.id,Page.site_id,Page.alias,Site.alias',
'recursive' => 1
)
);
// build array of data needed to make the paths
$page_data = array();
foreach($pages as $page){
$page_data[ $page['Page']['id'] ] = array(
'page_id' => $page['Page']['id'],
'page_alias' => $page['Page']['alias'],
'site_id' => $page['Page']['site_id'],
'site_alias' => $page['Site']['alias'],
'level' =>
(substr_count($treelist[$page['Page']['id']],'/') )
);
}
$path = array();
$prev_level = 0;
$prev_site = NULL;
$routes = array();
foreach($treelist as $pid => $level){
// snip logic that builds the proper URLs for each page
// and saves these for later use
}
// Sort routes in reverse key order so that the more specific
routes
come first
krsort($routes);
foreach($routes as $url => $route_data){
Router::connect($url,$route_data);
}
}
I'm definitely not doing this in the most efficient manner possible,
but it works and I can fix up the quirks later on. I'm pretty sure if
I actually understood how the tree behaviour works I could just get
the data I want in a single query ... but that can wait until later.
Like you said, "needs refactoring".
On Feb 5, 10:28 am, brian <[email protected]> wrote:
> On Thu, Feb 5, 2009 at 9:50 AM, Dwayne <[email protected]> wrote:
>
> > Yeah, I'm a bit iffy on taking specific actions on the pages as well,
> > but they aren't static content. I need some way of interacting with
> > the controllers that modify the content that won't leave the end user
> > confused as to which page their changes will show up on. But that
> > feature is still somewhat provisional until I know for certain I can
> > do it without breaking everything.
>
> But this is for a CMS? Are you trying to make it so that the user can
> modify the views for certain controller actions? That might be tricky.
>
> > Thanks for the code snippets, the SQL in particular should prove
> > extremely helpful.
>
> Keep in mind that I did that quite a while ago. It definitely needs
> refactoring.
>
> > While I'd rather not push the find logic into one
> > of my models (since that would require a route that gobbles up
> > everything) I'll probably end up trying something similar in the
> > routes file. What I'm hoping is that I'll have access to my Model at
> > that point (or at least be able to build the necessary access). If I
> > do, I'll be able to use the Tree behavior to build a list of all the
> > current paths and then connect those paths directly to the correct
> > controller/action/etc. Then I'd just need to figure out how to cache
> > these results until the page hierarchy changes. (This is an I idea I
> > had while I was asleep, so I won't be surprised if it turns out to not
> > work at all. In that case I probably will end up pushing the entire
> > path into a find method on the model.)
>
> Brother, I know all about trying to implement ideas that came to me in
> my sleep. Sometimes, they do actually work!
>
> Sometimes.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---