Single Pages and controller methods

Permalink 2 users found helpful
Hi,

so I have got a single page called myusers, myusers has a view in /single_pages/myusers and it also has a controller at /controller/myusers.php

This is fine but what I could like to know is the concrete5 way to call the methods.

I have 3 actions view, edit and delete. Should I use one view with one controller and call the method via the url? if so how do I control the html output on the singel page? Using a switch statement?

Or is it better to create delete.php and edit.php single_pages and a controller for each one?

View Replies:
olliephillips replied on at Permalink Reply
olliephillips
If your single page is registered as myusers.php, the URI will be yoursite.com/myusers. The nice thing about Concrete5 is that if you pass edit or delete as the next part of the URI, you'll be running the controller's methods.

e.g yoursite.com/myusers/edit, yoursite.com/myusers/delete

You can use a Concrete5 method to simplify your code

e.g $this->action('myusers', 'edit') = url to yoursite.com/myusers/edit

Hope that helps
roscoeh replied on at Permalink Reply
Thanks, that is great but how do I control the html (tables of users for example) on the single page? if I had a big chunk of html to output on the /view page how do I return that to the page? Is there a concrete5 way to do this?

I am using

$this->set('my_variable', $somevalue); for variables. Wondering what other inbuilt methods I can use and best practice is?

Thanks again.
JohntheFish replied on at Permalink Reply
JohntheFish
Its similar to blocks.

From any of the action functions in your controller you can use $this->set() to set up data for the view of the single page. However, in single page controllers while view() is the default action the single page view is also rendered after any action returns.

In the single page view you can end up with a if/else or switch/case depending on what the controller has set(). If the single page view has several disparate modes, you can split it up with elements and pass relevant data on.
mkly replied on at Permalink Reply
mkly
I'm not totally sure what you are looking for but this might be of some help.

In your controller you will typically have stuff like this.
public function on_start() {
  // do $this->set() stuff for things that will be
  // on every display
  // typically lots of $this->set()
}
public function view() {
  // does stuff for the default view
  // basically this is your search list etc(index)
  // Usually you look for a get/post value and display
  // your list based on that
  $this->set('view', 'index');
}
public function edit() {
  // does stuff for displaying the edit form
  $this->set('view', 'edit');

Then in your view.php of whatever you are calling that single page if not controller.php switch on it however you prefer
<?php if($view === 'index'): ?>
  <p>This is the index</p>
<?php endif; ?>
<?php if($view === 'edit'): ?>
  <p>This is the edit</p>
<?php endif; ?>

Now if you can hear you college professor/roomate who writes Rails code screaming that this is an MVC tragedy and horrible and OMG OH NOES!!!11! then you can call actually use separate view files from within the controllers like this in your controller
public function view() {
  // do stuff
  $this->render('/dashboard/your_thing_directory/view');
}

etc... and then you can use separate files and not have the switching. But it's less common around these parts.
JohntheFish replied on at Permalink Reply
JohntheFish
@mkly

I didn't know about that last trick with $this->render(). Will have to seek out more info on it as it could save me from a blossoming sprawl of switching and elements.

Thanks
ScottC replied on at Permalink Reply
ScottC
It does, but you get some problems with uninstalling an reinstalling unless you clear cache, it works really well except for when it doesn't and I haven't yet spent the time to find out why it doesn't.

-Scott
mkly replied on at Permalink Reply
mkly
I think this kind of talks about the genesis of it etc
https://github.com/concrete5/concrete5/pull/147...
JohntheFish replied on at Permalink Reply
JohntheFish
Ahh, new in 5.5, so that's why I hadn't seen it before.
roscoeh replied on at Permalink Reply
I an very interested in this method.

public function view() {
// do stuff
$this->render('/dashboard/your_thing_directory/view');
}


Is this possible

public function delete () {
// do stuff
$this->render('/dashboard/your_thing_directory/delete');
}

Where delete.php in the same folder is the view for the delete method?