Thanks both of you for your input.

All my mod_rewrite stuff is working properly.

I was looking for a way to circumvent putting a "Router::connect...."
for each static page I have. Some of my sites have 100-150 static
pages. I'm looking for a way to dynamically build the routes for all
the pages.

My script I wrote does work, I was just looking for input from others
on ways to optimize my code a little, or to make sure I was using best
practices.

-Jesse

On Jan 16, 5:38 pm, brian <[email protected]> wrote:
> In addition to clarkphp's comment, if you have static files inside
> directories, ie. views/pages/about/foo.ctp
>
> Router::connect('/about', array('controller' => 'pages', 'action' =>
> 'display', 'about/index'));
> Router::connect('/about/*', array('controller' => 'pages', 'action' =>
> 'display', 'about'));
>
> On Fri, Jan 16, 2009 at 4:01 PM, clarkphp <[email protected]> wrote:
>
> > Jesse,
> > Are you wanting to access static pages without seeing the word "pages"
> > in the URL?
> > You could edit app/config/routes.php to have lines like this in it:
>
> > Router::connect('/about', array('controller' => 'pages', 'action' =>
> > 'display', 'about'));
> > Router::connect('/join', array('controller' => 'pages', 'action' =>
> > 'display', 'join'));
>
> > Then if someone accesses your site withhttp://yoursite/aboutthey see
> > the page at app/views/pages/about.php
> > An access usinghttp://yoursite/joinwould display the join page at
> > app/views/pages/join.php
>
> > Are you using mod_rewrite in Apache?  Or am I completely missing your
> > intentions?
> > - Clark
>
> > On Jan 16, 10:19 am, Jesse <[email protected]> wrote:
> >> I know this comes up a lot. I've always fixed this before (1.1) with a
> >> core hack. I thought that as I moved my sites to 1.2 now I would come
> >> up with a little better solution.
>
> >> I'm not sure if this is the best solution but it works for me. I want
> >> others thoughts and opinions for this solution.
>
> >> THE BASICS:
> >> Use a class within the routes config to pull all files from the "view/
> >> pages" directory. Loop through this to account for directories and
> >> such. Store these in array (do a little hygiene). Then, loop through
> >> this array and create a custom route for each page.
>
> >> It works for me, but I am still thinking there is a better way of
> >> integrating this into cake. I'll provide the code I'm using on my dev
> >> box below. Any suggestions are welcome....
>
> >> /////Component that does all the work....... (should this not be a
> >> component?)
>
> >> <?php
>
> >> class PagesRoutesConfigComponent extends Object {
> >>     var $default_dir;
> >>     var $pages;
> >>     var $dirs;
> >>     var $deleted_dirs;
>
> >>     function __construct(){
> >>         $this->default_dir = '/users/jesseainskeep/sites/inskeep-
> >> photography/app/views/pages/';
> >>         $this->pages = array();
> >>         $this->dirs = array();
> >>         $this->deleted_dirs = array();
>
> >>         //run through default directory
> >>         $this->getPagesFromDir($this->default_dir);
>
> >>         //funny way or writing this... this populates the pages array
> >> and jumps through each directory it sees
> >>         $exit = true;
> >>         while ($exit){
> >>             //run through array...
> >>             foreach ($this->dirs as $key=>$value){
> >>                 $value = $value . "/";
> >>                 $this->getPagesFromDir($value);
> >>                 $this->deleted_dirs[] = $value;
> >>                 unset($this->dirs[$key]);
> >>             }
>
> >>             if (empty($this->dirs)){
> >>                 $exit = false;
> >>             }
> >>         }
>
> >>         //kill the full path attached to each "page"
> >>         foreach ($this->pages as $key=>$value){
> >>             $this->pages[$key] = str_replace($this->default_dir, '',
> >> $value);
> >>         }
>
> >>     }
>
> >>     function getPagesFromDir($sentDir){
> >>         $search = array('.ctp', '.thtml');
> >>         $replace = '';
> >>         if (is_dir($sentDir)) {
> >>         if ($dh = opendir($sentDir)) {
> >>             while (($file = readdir($dh)) !== false) {
> >>                 if (is_file($sentDir . $file)){
> >>                     $file = str_replace($search, $replace, $file);
> >>                     $this->pages[] = $sentDir . $file;
> >>                 }elseif(is_dir($sentDir . $file) && $file != "." &&
> >> $file != ".." ){
> >>                     $this->dirs[] = $sentDir . $file;
> >>                 }
> >>             }
> >>             closedir($dh);
> >>             }else{
> >>                 echo "Cannot open sent 'dir'!";
> >>             }
> >>         }else{
> >>             echo "Sent 'dir' not available!";
>
> >>         }
> >>     }
>
> >>     function printAll(){
> >>         echo "<h1>Pages</h1>";
> >>         print_r($this->pages);
> >>         echo "<br /><br />";
>
> >>         echo "<h1>Directories</h1>";
> >>         print_r($this->dirs);
> >>         echo "<br /><br />";
>
> >>         echo "<h1>Deleted Directories</h1>";
> >>         print_r($this->deleted_dirs);
> >>         echo "<br /><br />";
> >>     }
>
> >> }
>
> >> ?>
>
> >> ////Code in routes file...
> >> //read all files in pages directory and create custom route for each
> >> one
> >>     include_once('/users/jesseainskeep/sites/inskeep-photography/app/
> >> controllers/components/pages_routes_config.php');
> >>     $worker = new PagesRoutesConfigComponent();
> >>     foreach ($worker->pages as $page){
> >>         Router::connect($page, array('controller' => 'pages', 'action'
> >> => 'display', ltrim($page, '/')));
>
> >>     }
>
> >> As I said, this is my first stab and there will for sure need to be
> >> revisions (dynamically pulling Cake path, auto loading component vs.
> >> doing include, ect).
>
> >> This works good on my machine, but I'm sure there are problems.
> >> Suggestions welcome!

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to