Ah that works great!
It was the creating/adapting of te model correcty in the generic class
part where I got stuck :-) I did look at the scaffold controller but
didn't knew exactly what fields to set.
Thanks a lot!
On 10 mrt, 15:20, grigri <[EMAIL PROTECTED]> wrote:
> You could try something like this:
>
> routes.php
>
> Router::connect('/:pseudo/:action/*', array('controller' =>
> 'generic'), array('pseudo' => '/^(news|eggs|bacon|spam)$/));
>
> controllers/generic_controller.php
>
> class GenericController extends AppController {
> var uses = array();
>
> function beforeFilter() {
> // Get the pseudo-controller from the params
> $this->pseudo = $this->params['named']['pseudo'];
> // Set the various internal variables
> $this->modelClass = Inflector::classify($this->pseudo);
> $this->modelKey = Inflector::underscore($this->modelClass);
> $this->viewPath = $this->pseudo;
> // Load the model normally
> $this->loadModel($this->modelClass);
> }
>
> function view($id) {
> $this->set('data', $this->{$this->modelClass}->findById($id));
> }
>
> function edit($id = null) {
> if (!empty($this->data)) {
> $this->{$this->modelClass}->create();
> $this->{$this->modelClass}->id = $id;
> if ($this->{$this->modelClass}->save($this->data)) {
> // blablabla as normal
> }
> }
> else {
> $this->data = $this->{$this->modelClass}->findById($id);
> }
> }
>
> }
>
> So calling /news/edit/1 will be handled with generic_controller, with
> its internal variables set to:
>
> pseudo: news
> modelClass: News
> modelKey: news
> viewPath: news [this is the subfolder where the view files are stored]
>
> On Mar 10, 1:32 pm, Marcel <[EMAIL PROTECTED]> wrote:
>
> > Hello
>
> > Is there a way to create a generic crud controller shared by some
> > models? But there's a catch: I don't know before hand what the models
> > will be.
>
> > What I would like to have is something like this:
>
> > /news => dispatch to generic_controller.php
> > /news/edit/1 => dispatch to generic_controller.php, action edit
> > etc...
>
> > By default, cake searches for a file named news_controller.php, but I
> > want to change that to generic_controller.php
>
> > Is that possible?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" 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
-~----------~----~----~----~------~----~------~--~---