Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 08b1edc

Browse files
Router class
1 parent ef002d3 commit 08b1edc

File tree

2 files changed

+114
-1
lines changed

2 files changed

+114
-1
lines changed

‎index.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
11
<?php
2-
echo $_SERVER['REQUEST_URI'];
2+
require 'system/core/Router.php';
3+
4+
$router = new Router();
5+
echo '<pre>';
6+
print_r($router->getUri());
7+
echo '</pre>';
8+
9+
$controlador = $router->getController();
10+
$method = $router->getMethod();
11+
$param = $router->getParam();
12+
echo "Controlador: {$controlador} </br>";
13+
echo "Método: {$method} </br>";
14+
echo "Param: {$param} </br>";

‎system/core/Router.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
/**
3+
* Identificacion de la URI
4+
*/
5+
class Router
6+
{
7+
/**
8+
* @var string
9+
*/
10+
public $uri;
11+
12+
/**
13+
* @var string
14+
*/
15+
public $controller;
16+
17+
/**
18+
* @var string
19+
*/
20+
public $method;
21+
22+
/**
23+
* @var string
24+
*/
25+
public $param;
26+
27+
/**
28+
* Inicializa los atributos
29+
*/
30+
public function __construct()
31+
{
32+
$this->setUri();
33+
$this->setController();
34+
$this->setMethod();
35+
$this->setParam();
36+
}
37+
38+
/**
39+
* Asigna la uri completa
40+
*/
41+
public function setUri()
42+
{
43+
$this->uri = explode('/', $_SERVER['REQUEST_URI']);
44+
}
45+
46+
/**
47+
*Asigna el nombre del controlador
48+
*/
49+
public function setController()
50+
{
51+
$this->controller = $this->uri[2] === '' ? 'Home' : $this->uri[2];
52+
}
53+
54+
/**
55+
* Asigna el nombre del metodo
56+
*/
57+
public function setMethod()
58+
{
59+
$this->method = ! empty($this->uri[3]) ? $this->uri[3] : 'exec';
60+
}
61+
62+
/**
63+
* Asigna el valor del parametro si existe
64+
*/
65+
public function setParam()
66+
{
67+
$this->param = ! empty($this->uri[4]) ? $this->uri[4] : '';
68+
}
69+
70+
/**
71+
* @return $uri
72+
*/
73+
public function getUri()
74+
{
75+
return $this->uri;
76+
}
77+
78+
/**
79+
* @return $controller
80+
*/
81+
public function getController()
82+
{
83+
return $this->controller;
84+
}
85+
86+
/**
87+
* @return $method
88+
*/
89+
public function getMethod()
90+
{
91+
return $this->method;
92+
}
93+
94+
/**
95+
* @return $param
96+
*/
97+
public function getParam()
98+
{
99+
return $this->param;
100+
}
101+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /