model view controller - Parameters in Custom Router PHP

631

I am trying to create a custom router. This is what I have so far:

<?php
/**
 * Created by PhpStorm.
 * User: antony
 * Date: 5/30/16
 * Time: 3:31 PM
 */
namespace Fab\Router;

class Router
{
    private $_getUri = array();
    private $_getController = array();
    private $_getMethod = array();
    private $_postUri = array();
    private $_postController = array();
    private $_postMethod = array();

public function __construct()
{
}

/**
 * Build a collection of internal GET URLs to look for
 * @param $uri - The url that the user types in the browser
 * @param $controller - The controller that will handle the url
 * @param $method - The method of the controller that will run
 */
public function get($uri, $controller, $method)
{
    $this->_getUri[] = $uri;
    $this->_getController[] = $controller;
    $this->_getMethod[] = $method;
}

/**
 * Build a collection of internal POST URLs to look for
 * @param $uri - The url that the user types in the browser
 * @param $controller - The controller that will handle the url
 * @param $method - The method of the controller that will run
 */
public function post($uri, $controller, $method)
{
    $this->_postUri[] = $uri;
    $this->_postController[] = $controller;
    $this->_postMethod[] = $method;
}

public function submit()
{

    if ($_SERVER['REQUEST_METHOD'] === 'GET') {

        var_dump($_SERVER['REQUEST_URI']);
        echo "\n";  

        $path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); //get the url

        var_dump($path);

        foreach ($this->_getUri as $key => $value)
        {
            if (preg_match("#^$value$#", $path))
            {
 //                    echo $key . ' => ' . $value; //See what the $path returns

                //Instantiate Controller
                $controller = 'Fab\Controllers\\' . $this->_getController[$key];
                $controller = new $controller();

                //Call the appropriate method
                $method = $this->_getMethod[$key];
                $controller->$method();
            }
        }
    } elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {

        $path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); //get the url

        foreach ($this->_postUri as $key => $value)
        {
            if (preg_match("#^$value$#", $path))
            {
                //echo $key . ' => ' . $value; //See what the $path returns

                //Instantiate Controller
                $controller = 'Fab\Controllers\\' . $this->_postController[$key];
                $controller = new $controller();

                //Call the appropriate method
                $method = $this->_postMethod[$key];
                $controller->$method();
            }
        }
    }

}

}

This allows me to say in myindex.php something like:

$router->get('/portfolio', 'MainController', 'portfolio');

What I want to do now is to create an individual page for every item in the portfolio and give it a unique URL. For example, if I have the item 'dinosaur' in my portfolio, I want to be able to say:$router->get('/portfolio/dinosaur', 'MainController', 'searchIfDinosaurExistsInDatabase');

So generally, I would like to have in my index sth like:$router->get('/portfolio/{item}', 'MainController', 'searchInDbForItem');

How can I modify my router in order to achieve this?

146

Answer

Solution:

You need to add an regular expression to route. For example like this (it's a very simple example):

$router->get('/portfolio/[\w\d]+', 'MainController', 'searchInDbForItem');`

And you need usepreg_match for comparison a route and an url.

It's a very simple example of router https://github.com/newage/example/blob/master/core/Route/HttpRoute.php

People are also looking for solutions to the problem: php - Calculate price form excel javascript

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.