@
@
@atambalasi2015-12-14 09:17:31
PHP
@atambalasi, 2015-12-14 09:17:31

How to register routes?

For the purposes of practice, I implement routing. I can't solve one problem.
There is a file in it that registers routes

use Core\Http\RouteRegister;

Route::get('/ma', '[email protected]');
Route::get('/admin/add/video/google/high/:cat_id/:video_id/:auth', '[email protected]');

Here is the Route class
namespace Core\Http;

use Core\Http\RouteRegister;

class Route
{
  public $url;
  public $route;
  private $generedRoute;
  private $className;
  private $action;
  private $paramsCount;
  private $paramsCountRoute;
  private $slashCount;
  private $slashCountUrl;
  private $params = null;
  
  public static function initRoute($route, $classAction)
  {
    $init = new Route();
    $init->route = $route;
    //$init->className = $classAction;
    $init->url = $init->getURL();
    $init->slashCount = $init->getParamsAndSlashesCount('slash');
    $init->slashCountUrl = $init->getParamsAndSlashesCount('slash_url');
    $init->paramsCount = $init->getParamsAndSlashesCount();
    $init->generedRoute = $init->getGeneredUrl();
    $init->params = $init->getParams();
    $init->paramsCountRoute = $init->getParamsNameFromRoute(true);
    
    /*$regiter = new RouteRegister();
    $register->url = $init->url;
    $regiter->route = $init->route;
    $regiter->classAction = $classAction;*/
    return $init;
  }
  public static function get($route, $classAction)
  {
    $init = self::initRoute($route, $classAction);
    if(($init->slashCount + 1) == $init->slashCountUrl)
    {
      $init->slashCountUrl = $init->slashCountUrl - 1;
    }elseif(($init->slashCount - 1) == $init->slashCountUrl)
    {
      $init->slashCountUrl = $init->slashCountUrl + 1;
    }
    echo $init->url . "<br>";
    echo $init->generedRoute;
    
    if( $init->url == $init->generedRoute /*&& $init->slashCount == $init->slashCountUrl*/)
    {
       $tempArray = explode("@", $classAction);
       $className = $tempArray[0];
       $action = $tempArray[1];
       $cls =   'Controllers\\' . $className ;
       $obj = new $cls;
      //if(count($init->params))
      //{
        $obj->$action($init->params);
      //}else
      //{
        //$obj->$action();
        
      //}
    }else
    {
      //echo "The route as " . $init->url;
      echo "<h1> NOT FOUND </h1>";
    }
    /*for($i=0; $i < count($paramsArray); $i++)
    {
      if($i == 0)
      {
        $params[$paramsArray[$i]] = $paramsArray[$i + 1];
      }elseif($i%2 == 0)
      {
        $params[$paramsArray[$i]] = $paramsArray[$i + 1];
      }		
    }*/
    //exit();
  }
  public function getParams()
  {
    
    $paramsArray = $this->getParamsArray();
    $paramsCount = $this->getParamsAndSlashesCount();
    $keyName 	 = $this->getParamsNameFromRoute();
    $params 	 = [];

    for($i=0; $i < $this->paramsCount; $i++)
    {
      if(!isset($paramsArray[$i]) || empty($paramsArray[$i])) 
      {		
        echo "<h3 style='color:red'> You lost param!! Created route " . $this->route . "<h3>";
        echo "<h3 style='color:red'> Your url: " . $this->url . "<h3>";
        exit();
      }
      $params[$keyName[0][$i]] = $paramsArray[$i];
    }
    return $params;
  }
  public function getGeneredUrl()
  {
    $url = $this->getURL();
    $paramsArray = $this->getParamsArray();
    $countSlashes = $this->getParamsAndSlashesCount('slash');
    $paramsCount = $this->getParamsAndSlashesCount();
    if($paramsCount == 0)
    {
      return $url;
    }else
    {
      $routeArray = explode('/', $this->route);
      $generedURLArray = array_slice($routeArray, 0, ($countSlashes - $this->paramsCount) + 1);
      array_push($generedURLArray, implode('/', $paramsArray));
    
      return  implode('/', $generedURLArray);
    }
    

  
  }
  public function getParamsArray()
  {
    $countSlashes = $this->getParamsAndSlashesCount('slash');
    $paramsCount = $this->getParamsAndSlashesCount();
    $urlArray = explode('/', $this->url);
    $paramsArray = array_slice($urlArray, ($countSlashes - $this->paramsCount) + 1);
    //echo count($paramsArray);
    return $paramsArray;
    
    
  }
  public function getURL()
  {
    return $_SERVER['REQUEST_URI'];
  }
  public function getParamsAndSlashesCount($which = 'params')
  {
    if($which == 'params')
    {
      return preg_match_all("/(:)\w+/", $this->route, $out);
    }elseif($which == 'slash')
    {
      return 	preg_match_all("/\//", $this->route);
    }elseif($which == 'slash_url')
    {
      return 	preg_match_all("/\//", $this->url);
    }
  
  }
  public function getParamsNameFromRoute($count = false)
  {
    if(!$count)
    {
      preg_match_all("/(:)\w+/", $this->route, $out);
      return $out;
    }elseif($count)
    {
      return preg_match_all("/(:)\w+/", $this->route);
    }
  }
}

Here is the index file. Here I include the routes.php file
require_once '../core/autoload.php';
require_once '../core/routes.php';

use Models\Users;
use Controllers\Controller;
use Core\Http\RouteRegister;

The router class defines the requested page.
> hooks controller classes
> creates instances of page controllers and invokes the actions of those controllers.
But it will call all the routes from the routes.php file. He must do so. Because I include the routes.php file in index.php and pass the get method call as many routes as defined in the file. And how to register routes so that only the route I need is called. That is, for example, I type
site.loc/ma in the address and
now it calls bigTester and test actions.
I need the call to be only bigTester.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question