Thanks for your feedback Denny, Ben and Stuart - good food for thought!

I think some sort of handling logic in Root's default action to test for the 
existence of template and error or 404 if it doesn't - is the way forward.

Many thanks,
Anthony


From: Stuart Watt [mailto:[email protected]]
Sent: 11 October 2010 19:35
To: The elegant MVC web framework
Subject: Re: [Catalyst] Displaying template files without adding new controller 
actions

If it's a TT file you need (i.e., not static), along with appropriate 
processing, you can make a variation on Ben's suggestion by using the 
$c->request->arguments, and putting the file name into $c->stash->{template} 
before handing off to the TT view, which is likely to be the default. This is a 
slight variation on ben's suggestion, which will only hit the file system when 
looking at the right part of the URI space.

I've not tested this, but it'd just look something like:

sub dynamic : Local {
  my ($self, $c) = @_;
  my $args = $c->request->arguments();
  $c->stash->{template} = join('/', @$args);
};

This omits the error handling (assumes TT will take care of that) but should 
only handle requests that begin /dynamic. The arguments() method returns 
additional stuff after "/dynamic" in the requesting URL.

I do something similar in my code (it's not precisely the same, as I retrieve 
the file contents through a model) and it works pretty well for us.

For security, you probably want to remove stuff like "..", just to be safe.

All the best
Stuart


On 2010-10-11, at 11:14 AM, Ben van Staveren wrote:


The other way to do it (and this is hackish), would be something like this:

...


This is, however, very non-optimal since you are hitting the filesystem for 
every request that wasn't satisfied by an existing action. Like I said, you can 
either let the webserver serve up static pages. If you are worried about how 
the URL looks in the browser, some mod_rewrite magic will fix that for you, but 
that's a subject for another list :)

...

Anthony Gladdish wrote:


I want to add quite a few .tt2 pages to the app, in various directories.
I don't want to have to create a Controller action per added template page ( 
this is the only way I know of adding a web page to a Catalyst app ).
Every time I add a new page I'll have to add a new action to represent that 
page then restart the server. There must be a way to add as many template pages 
as required, and have a single Controller action display the web page if it's 
corresponding template exists, otherwise 404.

...

--
Ben van Staveren
phone: +62 81 70777529
email: [email protected]<mailto:[email protected]>

_______________________________________________
List: [email protected]
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/[email protected]/
Dev site: http://dev.catalyst.perl.org/

Reply via email to