0

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 my index.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?

padawanTony
  • 1,348
  • 2
  • 22
  • 41

1 Answers1

1

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 use preg_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

newage
  • 899
  • 7
  • 18
  • This has been working great for so long! Now, I need to have URs with Greek characters; so I would need to add sth like `[α-ωΑ-Ω]`. Do you know how I could do that here? – padawanTony Aug 21 '16 at 09:31
  • @padawanTony You can try `{Greek}` characters bundle or `[α-ωΑ-Ω]`. Full letter solution `[A-ZA-zΑ-Ωα-ωίϊΐόάέύϋΰήώ]` – newage Aug 22 '16 at 07:38
  • Since these don't work, I created a new issue in which I explain the situation better. Take a look if you want: http://stackoverflow.com/questions/39076407/regex-greek-characters-in-url – padawanTony Aug 22 '16 at 09:54
  • @padawanTony For encoding you need to use `urldecode()` – newage Aug 22 '16 at 10:04