Skip to main content

Creating a page with a custom URL (v2)

You can create a custom URL for a page (as well as template, layout and theme) by adding it to your site's routing table, which is basically a list of URLs and the components needed to build them. In this example, we will add a custom URL for a staff directory page, 'yoursite.com/staff':

  • Create a content item with the data entry form and take note of its ID number. You can use any kind of content, but if you don't want the page to appear in your news feed then choose the 'static' content type.
  • Edit the file trust_path/libraries/tuskfish/routingTable.php. Make a copy of the base route '/', which is the default route for displaying content, and add it to the end of the table, changing the path as you require, in this example, '/staff/'. Also change the name of the controller to 'Staff'.
'/staff/' => new Route(
        '\\Tfish\\Content\\Model\\Listing',
        '\\Tfish\\Content\\ViewModel\\Listing',
        '\\Tfish\\View\\Listing',
        '\\Tfish\\Content\\Controller\\Staff',
        false),
  • Create a new 'Staff.php' Controller file in trust_path/libraries/tuskfish/class/Tfish/Content/Controller/, setting the $id variable in display() to match your content (copy and customise the code below).
  • You can also set the theme for this page, or specify an alternative content template or page layout for this page, so long as you have created them. You have complete control.
class Staff
{
    private $model;
    private $viewModel;

    public function __construct($model, $viewModel)
    {
        $this->model = $model;
        $this->viewModel = $viewModel;
    }

    public function display(): array
    {
        // Configure the parameters below to match your content
        $id = 4;
        $cacheParams = ['page' => 'staff'];
        // $this->viewModel->setTemplate('someOtherTemplate');
        // $this->viewModel->setLayout('someOtherLayout');
        // $this->viewModel->setTheme('someOtherTheme');
    
        $this->viewModel->setId($id);
        $cacheParams['id'] = $id;
        $this->viewModel->displayObject();

        return $cacheParams;
    }
}

Accessing yoursite.com/staff will now display the content item associated with $id.

Copyright, all rights reserved.