The default action

Now that we have a page with a list of the products, it's time to create the separate product pages.

The pages will be located under http://localhost/products/xxx where xxx is the product id.

Since there are two products with the id:s foo and bar, a solution might be to create a fooAction and a barAction in the ProductsController. While that may work, it's not a very flexible or future-proof solution.

Instead, we will use a special action called the default action.

Create the default action

Open Application/Controllers/ProductsController.php and add the defaultAction method:

class ProductsController extends Controller { public function indexAction(): View { $products = Product::getAll(); $this->setViewItem('Title', 'Products'); return new View($products); } public function defaultAction($action): string { return 'Action is ' . $action; } }

Check the result

Open the url http://localhost/products/foo and check the result:

Action is foo

This short example demonstrated the functionality of the default action: When no other suitable action method was found in the controller, the defaultAction will be invoked (if it exists) with the action as a parameter.

This behaviour is somewhat similar to the default case in a switch-statement.

To make an action method that literally matches default, as in http://localhost/default, the method name must be prefixed with an underscore, i.e. _defaultAction(). This also applies to the index action.

Find the product

Using the $action parameter in the default method, it's now possible to find the product and pass it to a view.

Modify the defaultAction method in Application/Controllers/ProductsController.php to look like this:

public function defaultAction($action): View { $product = Product::getById($action); $this->setViewItem('Title', $product->Name); return new View($product); }

Show the product

And as always when returning a view, create the view file.

Create a new file default.twig in the directory Application/Views/Products/ with the following content:

{% extends "sitelayout.twig" %} {% block content %} <h1>{{ Model.Name }}</h1> <p> Price: {{ Model.Price }} </p> <p> <a href="/products/">Back</a> </p> {% endblock %}

Check the result

Reload the http://localhost/products/foo page:

Product page for 24 inch LCD Monitor

Link to the products

Since the product pages seems to be working, it's time to make the links on the product list clickable.

Open Application/Views/Products/index.twig and modify the content to look like this:

{% extends "sitelayout.twig" %} {% block content %} <h1>Products</h1> <ul> {% for Product in Model %} <li><a href="/products/{{ Product.Id }}">{{ Product.Name }}</a></li> {% endfor %} </ul> {% endblock %}

Check the result

Open the url http://localhost/products/ and check the result:

Products page with product links

Try to click on the links to make sure it's working.

While it may seem that the products pages are working, there is still some error handing that needs to be done. This will be covered in the next part.

✎ Published in category get started