Once you have a single page and a controller, you can sending data from the controller into the view by using $this->set('key', $value);
from within your controller file which will make key
available as $key
in the view file (e.g.: <?= $key ?>
).
Example
Say we want to list photo albums on our media page. We have the PHP class Foo\Media\Album
and we'll retrieve a list of these album objects when browsing to our media page
http://www.example.com/media/
Here's our controller
namespace Application\Controller\SinglePage;
use Concrete\Core\Page\Controller\PageController;
class Media extends PageController
{
public function view()
{
$list = \Foo\Media\Album::getList();
$this->set('albums', $list);
}
}
That's it. The array of photo albums will now be available within the application/single_pages/media/view.php
template file in the local variable $albums
, since that's the name of the first parameter in our set()
method.
Let's take a look at our view.php
file. Here's how we can link to a different method in our list of albums
<ul>
<?php
foreach($albums as $album) { ?>
<li>
<a href="<?=URL::to('/media', 'view_album', $album->getAlbumID())?>"><?=$album->getAlbumName()?></a>
</li>
<?php } ?>
</ul>
For each entry in the $albums
array in the above example, we print out a list item with a link inside it. The three parameters of that link created by URL::to()
are:
- The path to the single page linked to
- The method we want to run on the single page controller in question
- One or more parameters we want to send into that method (e.g.:
view_album
). The method must be set up to take the number of parameters we give it. Parameters are just passed as additional PHP function parameters, separated by commas.
If you click on one of these links, the URL will look like this
http://www.example.com/media/view_album/4
If there's no view_album()
method in the media controller, or there is but it doesn't accept a parameter, the result will be a 404. Here's what the method might look like:
public function view_album($albumID = null)
{
$selectedAlbum = \Foo\Media\Album::getByID($albumID);
$this->set('albumID', $albumID);
$this->set('album', $selectedAlbum);
}
Now, the $albumID
variable and the $album
variable will be available in the media single page view template. Now might be a good time to explore rendering a different view based on the fact that our controller method is view_album
, rather than view
.
Submit back to the Current Page with Controller::action
If you want to run a particular method on the current page, you don't need to include the single page path in the URL::to method. Just use the action()
method on the $controller
parameter that is automatically in the local scope. Here's how a form can submit back to the current page's submit()
method in the controller:
<form method="post" action="<?=$controller->action('submit')?>">
</form>