⊗ppMVUFmRPA 9 of 35 menu

Using Parameters in Routes in MVC in PHP

Let's look at the application of the described on a more real-life example. Let our controller PageController contain an array of pages (this data should be provided by the model, but we haven't covered models yet, so let the data be stored in the controller for now):

<?php namespace Project\Controllers; use Core\Controller; class PageController extends Controller { private $pages; public function __construct() { $this->pages = [ 1 => 'page 1', 2 => 'page 2', 3 => 'page 3', ]; } } ?>

Let's create an action show, which will display a page with a specific number (id):

<?php namespace Project\Controllers; use Core\Controller; class PageController extends Controller { private $pages; public function __construct() { $this->pages = [ 1 => 'page 1', 2 => 'page 2', 3 => 'page 3', ]; } public function show() { // here we will output the page with a specific number } } ?>

Let when accessing the address /page/1/ the text of the first page is displayed, when accessing the address /page/2/ - the text of the second page, and so on. Let's create the corresponding route:

<?php use \Core\Route; return [ new Route('/page/:id/', 'page', 'show'), ]; ?>

Let's implement the described method show:

<?php namespace Project\Controllers; use Core\Controller; class PageController extends Controller { private $pages; public function __construct() { $this->pages = [ 1 => 'page 1', 2 => 'page 2', 3 => 'page 3', ]; } public function show($params) { echo $this->pages[ $params['id'] ]; // output the page by number } } ?>

Implement the controller UserController, containing the following array:

<?php $this->users = [ 1 => ['name'=>'user1', 'age'=>'23', 'salary' => 1000], 2 => ['name'=>'user2', 'age'=>'24', 'salary' => 2000], 3 => ['name'=>'user3', 'age'=>'25', 'salary' => 3000], 4 => ['name'=>'user4', 'age'=>'26', 'salary' => 4000], 5 => ['name'=>'user5', 'age'=>'27', 'salary' => 5000], ]; ?>

In the controller UserController, create an action show, which will output a user by a specific id. Let it be available at an address of the following type: /user/:id/.

In the controller UserController, create an action info, which will output the name or age of a specified user. Let this action be available at an address of the following type: /user/:id/:key/, where key will have the value 'name', 'age' or 'salary'.

In the controller UserController, create an action all, which will output a list of all users to the screen. Let this action be available at an address of the following type: /user/all/ (there will be no parameters here).

In the controller UserController, create an action first, which will output a list of the first N users to the screen. Let this action be available at an address of the following type: /user/first/:n/, where the parameter will contain the number of users to display on the screen.

English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline