Add a simple page

We will now add a simple "About" page to the website we installed previously.

Add an action method

Adding a new page is done by adding a new action method that returns a simple html-string. Open the Application/Controllers/IndexController.php file and add the aboutAction method below:

/** * The index controller class. */ class IndexController extends Controller { /** * The index action. * * @return View The view for this action. */ public function indexAction(): View { $this->setViewItem('Title', 'My website'); return new View(); } public function aboutAction(): string { return '<h1>About this website</h1>'; } }

Check the result

Use your browser to navigate to the url http://localhost/about. A very simple page should be displayed:

About this website

With a few exceptions, the name of the action method will automatically be mapped to the last part of the url. A request for the url http://localhost/about will invoke our new action method. Similarly, an action method with the name fooAction would be invoked for http://localhost/foo and so on.

It's not very impressive and certainly not a valid html-page, but this will be improved in the next parts of this tutorial.

✎ Published in category get started