NYCPHP Meetup

[nycphp-talk] Many pages: one script

Jon Baer jonbaer at jonbaer.com
Mon Aug 6 21:45:55 EDT 2007


Bare minimum w/ CakePHP (4 files/scripts: model, controller, view,  
route) + provided you have a db with a table called "articles":

1) /models/article.php
public class Article extends AppModel {}

2) /controllers/news_controller.php
public class NewsController extends AppController {
   function index($year, $month, $day) {
     $created = $year . "-" . $month . "-" . $day;
     $this->set('articles', $this->Article->findAllByCreated($date);
   }
}

3) /config/routes.php
$Route->connect ('/news', array('controller'=>'News',  
'action'=>'index'));

4) /app/views/news/index.php
<?php foreach($articles as $article): ?>
<?= $article['Article']['title'] ?>
<?php endforeach; ?>

mod_rewrite will push everything to index.php which will call a  
"Dispatcher" to handle the URL + follow what has been declared in the  
routes file.  I believe this would be almost the exact mechanism  
found in items like DispatchServlet in Spring, etc.  (although I  
think you can abstract more items).

- Jon

On Aug 6, 2007, at 6:54 PM, Elliotte Harold wrote:

> Kenneth Downs wrote:
>
>> Again, I'm not clear on what you are trying to serve.  We probably  
>> have to back up to the beginning and erase the assumption that PHP  
>> has a one-to-one correspondence between a URL (or page) and a PHP  
>> file.  Having erased that, we have to ask what kind of content you  
>> are trying to serve, then we have to look at PHP examples.
>
> Here's a simple example: a news site backed by a database. URLs like
>
> http://www.example.com/news/2007/07/05
> http://www.example.com/news/2007/07/06
> http://www.example.com/news/2007/07/07
> http://www.example.com/news/2007/07/08
> ...
>
> return pages which contain that day's headlines extracted from the  
> database.
>
> One script, no more, must handle all dates. (I don't really care if  
> there are 2 or 3 scripts, but I do not want to have to write a  
> separate page for each URL. The number of PHP scripts must be  
> finite and fixed. It should not increase with the number of URLs  
> the script services.)
>
> The only way I've ever seen this done in PHP is by using  
> mod_rewrite, though they're a couple of other interesting  
> suggestions in the thread I need to explore further. Do you have a  
> suggestion?
>
> -- 
> Elliotte Rusty Harold  elharo at metalab.unc.edu
> Java I/O 2nd Edition Just Published!
> http://www.cafeaulait.org/books/javaio2/
> http://www.amazon.com/exec/obidos/ISBN=0596527500/ref=nosim/ 
> cafeaulaitA/
> _______________________________________________
> New York PHP Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
>
> NYPHPCon 2006 Presentations Online
> http://www.nyphpcon.com
>
> Show Your Participation in New York PHP
> http://www.nyphp.org/show_participation.php

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20070806/6aaa658a/attachment.html>


More information about the talk mailing list