Simple router in PHP
up vote
1
down vote
favorite
Before I start writing my first MVC framework, I want to improve my code quality as much as I can, can anyone give me some constructive criticism on how I can improve this?
It is a simple router, built for simple use. It should be pretty simple what's going on, and the usage should be easy to picture. It's nothing major but works well for simple functionality using it.
<?php
class Router {
public $routes = ;
public $routeMethods = ;
public function get($route, $settings) {
$this->add($route, $settings, ['GET']);
}
public function post($route, $settings) {
$this->add($route, $settings, ['POST']);
}
public function add($route, $settings, $methods) {
$this->routes[$route] = $settings;
$this->routeMethods[$route] = $methods;
}
public function dispatch() {
$requestUrl = $_SERVER['QUERY_STRING'];
if (array_key_exists($requestUrl, $this->routes)) {
$route = $this->routes[$requestUrl];
$methods = $this->routeMethods[$requestUrl];
$method = $_SERVER['REQUEST_METHOD'];
if (!in_array($method, $methods)) {
echo 'Method ' . $method . ' not allowed.';
}
else {
$controller = $route['controller'];
if (class_exists($controller)) {
$class = new $controller(); // grab from di cache when we have one
$action = $route['action'];
if (method_exists($class, $action)) {
$class->$action();
}
else {
echo 'We couldn't find action '' . $action . ' in controller '' . $controller . ''';
}
}
else {
echo 'We couldn't find controller '' . $controller . ''';
}
}
}
else {
echo '404 not found';
}
}
}
php mvc url
add a comment |
up vote
1
down vote
favorite
Before I start writing my first MVC framework, I want to improve my code quality as much as I can, can anyone give me some constructive criticism on how I can improve this?
It is a simple router, built for simple use. It should be pretty simple what's going on, and the usage should be easy to picture. It's nothing major but works well for simple functionality using it.
<?php
class Router {
public $routes = ;
public $routeMethods = ;
public function get($route, $settings) {
$this->add($route, $settings, ['GET']);
}
public function post($route, $settings) {
$this->add($route, $settings, ['POST']);
}
public function add($route, $settings, $methods) {
$this->routes[$route] = $settings;
$this->routeMethods[$route] = $methods;
}
public function dispatch() {
$requestUrl = $_SERVER['QUERY_STRING'];
if (array_key_exists($requestUrl, $this->routes)) {
$route = $this->routes[$requestUrl];
$methods = $this->routeMethods[$requestUrl];
$method = $_SERVER['REQUEST_METHOD'];
if (!in_array($method, $methods)) {
echo 'Method ' . $method . ' not allowed.';
}
else {
$controller = $route['controller'];
if (class_exists($controller)) {
$class = new $controller(); // grab from di cache when we have one
$action = $route['action'];
if (method_exists($class, $action)) {
$class->$action();
}
else {
echo 'We couldn't find action '' . $action . ' in controller '' . $controller . ''';
}
}
else {
echo 'We couldn't find controller '' . $controller . ''';
}
}
}
else {
echo '404 not found';
}
}
}
php mvc url
_SERVER should be provided from outside. What if action shoul accept parameters?
– Vladimir Vukanac
yesterday
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Before I start writing my first MVC framework, I want to improve my code quality as much as I can, can anyone give me some constructive criticism on how I can improve this?
It is a simple router, built for simple use. It should be pretty simple what's going on, and the usage should be easy to picture. It's nothing major but works well for simple functionality using it.
<?php
class Router {
public $routes = ;
public $routeMethods = ;
public function get($route, $settings) {
$this->add($route, $settings, ['GET']);
}
public function post($route, $settings) {
$this->add($route, $settings, ['POST']);
}
public function add($route, $settings, $methods) {
$this->routes[$route] = $settings;
$this->routeMethods[$route] = $methods;
}
public function dispatch() {
$requestUrl = $_SERVER['QUERY_STRING'];
if (array_key_exists($requestUrl, $this->routes)) {
$route = $this->routes[$requestUrl];
$methods = $this->routeMethods[$requestUrl];
$method = $_SERVER['REQUEST_METHOD'];
if (!in_array($method, $methods)) {
echo 'Method ' . $method . ' not allowed.';
}
else {
$controller = $route['controller'];
if (class_exists($controller)) {
$class = new $controller(); // grab from di cache when we have one
$action = $route['action'];
if (method_exists($class, $action)) {
$class->$action();
}
else {
echo 'We couldn't find action '' . $action . ' in controller '' . $controller . ''';
}
}
else {
echo 'We couldn't find controller '' . $controller . ''';
}
}
}
else {
echo '404 not found';
}
}
}
php mvc url
Before I start writing my first MVC framework, I want to improve my code quality as much as I can, can anyone give me some constructive criticism on how I can improve this?
It is a simple router, built for simple use. It should be pretty simple what's going on, and the usage should be easy to picture. It's nothing major but works well for simple functionality using it.
<?php
class Router {
public $routes = ;
public $routeMethods = ;
public function get($route, $settings) {
$this->add($route, $settings, ['GET']);
}
public function post($route, $settings) {
$this->add($route, $settings, ['POST']);
}
public function add($route, $settings, $methods) {
$this->routes[$route] = $settings;
$this->routeMethods[$route] = $methods;
}
public function dispatch() {
$requestUrl = $_SERVER['QUERY_STRING'];
if (array_key_exists($requestUrl, $this->routes)) {
$route = $this->routes[$requestUrl];
$methods = $this->routeMethods[$requestUrl];
$method = $_SERVER['REQUEST_METHOD'];
if (!in_array($method, $methods)) {
echo 'Method ' . $method . ' not allowed.';
}
else {
$controller = $route['controller'];
if (class_exists($controller)) {
$class = new $controller(); // grab from di cache when we have one
$action = $route['action'];
if (method_exists($class, $action)) {
$class->$action();
}
else {
echo 'We couldn't find action '' . $action . ' in controller '' . $controller . ''';
}
}
else {
echo 'We couldn't find controller '' . $controller . ''';
}
}
}
else {
echo '404 not found';
}
}
}
php mvc url
php mvc url
edited 2 days ago
200_success
127k15148412
127k15148412
asked 2 days ago
Beefo
312
312
_SERVER should be provided from outside. What if action shoul accept parameters?
– Vladimir Vukanac
yesterday
add a comment |
_SERVER should be provided from outside. What if action shoul accept parameters?
– Vladimir Vukanac
yesterday
_SERVER should be provided from outside. What if action shoul accept parameters?
– Vladimir Vukanac
yesterday
_SERVER should be provided from outside. What if action shoul accept parameters?
– Vladimir Vukanac
yesterday
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
- set
public $routes
to private or protected if its jsut an internal resource of this class. Just use public if other classes/code explicitly should call the mothod/property
method_exists
doesn't check if the method is callable. But that might be important if you execute legacy code- Avoid
echo 'some text'
Better use exceptions and an exception handler - If you use
if (){} else{}
you could think about using the if for the termination condition and the function continues regulary if its ok - Your note about the dependency injection cache is good. Keep your idea. You are on the rigth way.
- I would recommend to use doc-blocks as annotation and for IDE autocompletion for all public functions as well as class properties. This will increase the readability
- You didn't said which version of PHP you are using. Switch to >= 7.1 because 7.0 will reach its end of life soon. So you should use return types and type hinting
New contributor
add a comment |
up vote
0
down vote
Expanding on the answer provided by unherz;
Validate the programmer
Sometimes we make mistakes so you should check the $settings
provided by the programmer are correct as you require them to pass at least two parameters action
& controller
so I would add a method validateRouteSettings
I also implemented unherz answer to show how early returns (exceptions in this case) would look
Naming
You call object methods "actions" which is in-correct, should try to be consistent in naming structures
Any way here an updated implementation
<?php
class Router {
protected $routes = ;
protected $routeMethods = ;
public function get(string $route, array $settings) :void {
$this->add($route, $settings, ['GET']);
}
public function post(string $route, array $settings) :void {
$this->add($route, $settings, ['POST']);
}
public function add(string $route, array $settings, array $methods) :void {
$this->validateRouteSettings($route, $settings);
$this->routes[$route] = $settings;
$this->routeMethods[$route] = $methods;
}
private function validateRouteSettings(string $route, array $settings) :bool
{
if(!isset($settings["controller"])){
throw new Exception("Missing controller for $route", 1);
}elseif(!isset($settings["method"])){
throw new Exception("Missing method for $route", 1);
}
return true;
}
public function dispatch() :void {
$requestUrl = $_SERVER['QUERY_STRING'];
if (!array_key_exists($requestUrl, $this->routes)) {
throw new Exception('404 not found', 1);
}
$route = $this->routes[$requestUrl];
$methods = $this->routeMethods[$requestUrl];
$method = $_SERVER['REQUEST_METHOD'];
if (!in_array($method, $methods)) {
throw new Exception('Method ' . $method . ' not allowed.', 1);
}
$controller = $route['controller'];
if (!class_exists($controller)) {
throw new Exception('We couldn't find controller '' . $controller . ''', 1);
}
$class = new $controller(); // grab from di cache when we have one
$method = $route['method'];
if (!method_exists($class, $method)) {
throw new Exception('We couldn't find method '' . $method . ' in controller '' . $controller . ''', 1);
}
$class->$method();
}
}
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
- set
public $routes
to private or protected if its jsut an internal resource of this class. Just use public if other classes/code explicitly should call the mothod/property
method_exists
doesn't check if the method is callable. But that might be important if you execute legacy code- Avoid
echo 'some text'
Better use exceptions and an exception handler - If you use
if (){} else{}
you could think about using the if for the termination condition and the function continues regulary if its ok - Your note about the dependency injection cache is good. Keep your idea. You are on the rigth way.
- I would recommend to use doc-blocks as annotation and for IDE autocompletion for all public functions as well as class properties. This will increase the readability
- You didn't said which version of PHP you are using. Switch to >= 7.1 because 7.0 will reach its end of life soon. So you should use return types and type hinting
New contributor
add a comment |
up vote
1
down vote
- set
public $routes
to private or protected if its jsut an internal resource of this class. Just use public if other classes/code explicitly should call the mothod/property
method_exists
doesn't check if the method is callable. But that might be important if you execute legacy code- Avoid
echo 'some text'
Better use exceptions and an exception handler - If you use
if (){} else{}
you could think about using the if for the termination condition and the function continues regulary if its ok - Your note about the dependency injection cache is good. Keep your idea. You are on the rigth way.
- I would recommend to use doc-blocks as annotation and for IDE autocompletion for all public functions as well as class properties. This will increase the readability
- You didn't said which version of PHP you are using. Switch to >= 7.1 because 7.0 will reach its end of life soon. So you should use return types and type hinting
New contributor
add a comment |
up vote
1
down vote
up vote
1
down vote
- set
public $routes
to private or protected if its jsut an internal resource of this class. Just use public if other classes/code explicitly should call the mothod/property
method_exists
doesn't check if the method is callable. But that might be important if you execute legacy code- Avoid
echo 'some text'
Better use exceptions and an exception handler - If you use
if (){} else{}
you could think about using the if for the termination condition and the function continues regulary if its ok - Your note about the dependency injection cache is good. Keep your idea. You are on the rigth way.
- I would recommend to use doc-blocks as annotation and for IDE autocompletion for all public functions as well as class properties. This will increase the readability
- You didn't said which version of PHP you are using. Switch to >= 7.1 because 7.0 will reach its end of life soon. So you should use return types and type hinting
New contributor
- set
public $routes
to private or protected if its jsut an internal resource of this class. Just use public if other classes/code explicitly should call the mothod/property
method_exists
doesn't check if the method is callable. But that might be important if you execute legacy code- Avoid
echo 'some text'
Better use exceptions and an exception handler - If you use
if (){} else{}
you could think about using the if for the termination condition and the function continues regulary if its ok - Your note about the dependency injection cache is good. Keep your idea. You are on the rigth way.
- I would recommend to use doc-blocks as annotation and for IDE autocompletion for all public functions as well as class properties. This will increase the readability
- You didn't said which version of PHP you are using. Switch to >= 7.1 because 7.0 will reach its end of life soon. So you should use return types and type hinting
New contributor
New contributor
answered yesterday
unherz
1113
1113
New contributor
New contributor
add a comment |
add a comment |
up vote
0
down vote
Expanding on the answer provided by unherz;
Validate the programmer
Sometimes we make mistakes so you should check the $settings
provided by the programmer are correct as you require them to pass at least two parameters action
& controller
so I would add a method validateRouteSettings
I also implemented unherz answer to show how early returns (exceptions in this case) would look
Naming
You call object methods "actions" which is in-correct, should try to be consistent in naming structures
Any way here an updated implementation
<?php
class Router {
protected $routes = ;
protected $routeMethods = ;
public function get(string $route, array $settings) :void {
$this->add($route, $settings, ['GET']);
}
public function post(string $route, array $settings) :void {
$this->add($route, $settings, ['POST']);
}
public function add(string $route, array $settings, array $methods) :void {
$this->validateRouteSettings($route, $settings);
$this->routes[$route] = $settings;
$this->routeMethods[$route] = $methods;
}
private function validateRouteSettings(string $route, array $settings) :bool
{
if(!isset($settings["controller"])){
throw new Exception("Missing controller for $route", 1);
}elseif(!isset($settings["method"])){
throw new Exception("Missing method for $route", 1);
}
return true;
}
public function dispatch() :void {
$requestUrl = $_SERVER['QUERY_STRING'];
if (!array_key_exists($requestUrl, $this->routes)) {
throw new Exception('404 not found', 1);
}
$route = $this->routes[$requestUrl];
$methods = $this->routeMethods[$requestUrl];
$method = $_SERVER['REQUEST_METHOD'];
if (!in_array($method, $methods)) {
throw new Exception('Method ' . $method . ' not allowed.', 1);
}
$controller = $route['controller'];
if (!class_exists($controller)) {
throw new Exception('We couldn't find controller '' . $controller . ''', 1);
}
$class = new $controller(); // grab from di cache when we have one
$method = $route['method'];
if (!method_exists($class, $method)) {
throw new Exception('We couldn't find method '' . $method . ' in controller '' . $controller . ''', 1);
}
$class->$method();
}
}
add a comment |
up vote
0
down vote
Expanding on the answer provided by unherz;
Validate the programmer
Sometimes we make mistakes so you should check the $settings
provided by the programmer are correct as you require them to pass at least two parameters action
& controller
so I would add a method validateRouteSettings
I also implemented unherz answer to show how early returns (exceptions in this case) would look
Naming
You call object methods "actions" which is in-correct, should try to be consistent in naming structures
Any way here an updated implementation
<?php
class Router {
protected $routes = ;
protected $routeMethods = ;
public function get(string $route, array $settings) :void {
$this->add($route, $settings, ['GET']);
}
public function post(string $route, array $settings) :void {
$this->add($route, $settings, ['POST']);
}
public function add(string $route, array $settings, array $methods) :void {
$this->validateRouteSettings($route, $settings);
$this->routes[$route] = $settings;
$this->routeMethods[$route] = $methods;
}
private function validateRouteSettings(string $route, array $settings) :bool
{
if(!isset($settings["controller"])){
throw new Exception("Missing controller for $route", 1);
}elseif(!isset($settings["method"])){
throw new Exception("Missing method for $route", 1);
}
return true;
}
public function dispatch() :void {
$requestUrl = $_SERVER['QUERY_STRING'];
if (!array_key_exists($requestUrl, $this->routes)) {
throw new Exception('404 not found', 1);
}
$route = $this->routes[$requestUrl];
$methods = $this->routeMethods[$requestUrl];
$method = $_SERVER['REQUEST_METHOD'];
if (!in_array($method, $methods)) {
throw new Exception('Method ' . $method . ' not allowed.', 1);
}
$controller = $route['controller'];
if (!class_exists($controller)) {
throw new Exception('We couldn't find controller '' . $controller . ''', 1);
}
$class = new $controller(); // grab from di cache when we have one
$method = $route['method'];
if (!method_exists($class, $method)) {
throw new Exception('We couldn't find method '' . $method . ' in controller '' . $controller . ''', 1);
}
$class->$method();
}
}
add a comment |
up vote
0
down vote
up vote
0
down vote
Expanding on the answer provided by unherz;
Validate the programmer
Sometimes we make mistakes so you should check the $settings
provided by the programmer are correct as you require them to pass at least two parameters action
& controller
so I would add a method validateRouteSettings
I also implemented unherz answer to show how early returns (exceptions in this case) would look
Naming
You call object methods "actions" which is in-correct, should try to be consistent in naming structures
Any way here an updated implementation
<?php
class Router {
protected $routes = ;
protected $routeMethods = ;
public function get(string $route, array $settings) :void {
$this->add($route, $settings, ['GET']);
}
public function post(string $route, array $settings) :void {
$this->add($route, $settings, ['POST']);
}
public function add(string $route, array $settings, array $methods) :void {
$this->validateRouteSettings($route, $settings);
$this->routes[$route] = $settings;
$this->routeMethods[$route] = $methods;
}
private function validateRouteSettings(string $route, array $settings) :bool
{
if(!isset($settings["controller"])){
throw new Exception("Missing controller for $route", 1);
}elseif(!isset($settings["method"])){
throw new Exception("Missing method for $route", 1);
}
return true;
}
public function dispatch() :void {
$requestUrl = $_SERVER['QUERY_STRING'];
if (!array_key_exists($requestUrl, $this->routes)) {
throw new Exception('404 not found', 1);
}
$route = $this->routes[$requestUrl];
$methods = $this->routeMethods[$requestUrl];
$method = $_SERVER['REQUEST_METHOD'];
if (!in_array($method, $methods)) {
throw new Exception('Method ' . $method . ' not allowed.', 1);
}
$controller = $route['controller'];
if (!class_exists($controller)) {
throw new Exception('We couldn't find controller '' . $controller . ''', 1);
}
$class = new $controller(); // grab from di cache when we have one
$method = $route['method'];
if (!method_exists($class, $method)) {
throw new Exception('We couldn't find method '' . $method . ' in controller '' . $controller . ''', 1);
}
$class->$method();
}
}
Expanding on the answer provided by unherz;
Validate the programmer
Sometimes we make mistakes so you should check the $settings
provided by the programmer are correct as you require them to pass at least two parameters action
& controller
so I would add a method validateRouteSettings
I also implemented unherz answer to show how early returns (exceptions in this case) would look
Naming
You call object methods "actions" which is in-correct, should try to be consistent in naming structures
Any way here an updated implementation
<?php
class Router {
protected $routes = ;
protected $routeMethods = ;
public function get(string $route, array $settings) :void {
$this->add($route, $settings, ['GET']);
}
public function post(string $route, array $settings) :void {
$this->add($route, $settings, ['POST']);
}
public function add(string $route, array $settings, array $methods) :void {
$this->validateRouteSettings($route, $settings);
$this->routes[$route] = $settings;
$this->routeMethods[$route] = $methods;
}
private function validateRouteSettings(string $route, array $settings) :bool
{
if(!isset($settings["controller"])){
throw new Exception("Missing controller for $route", 1);
}elseif(!isset($settings["method"])){
throw new Exception("Missing method for $route", 1);
}
return true;
}
public function dispatch() :void {
$requestUrl = $_SERVER['QUERY_STRING'];
if (!array_key_exists($requestUrl, $this->routes)) {
throw new Exception('404 not found', 1);
}
$route = $this->routes[$requestUrl];
$methods = $this->routeMethods[$requestUrl];
$method = $_SERVER['REQUEST_METHOD'];
if (!in_array($method, $methods)) {
throw new Exception('Method ' . $method . ' not allowed.', 1);
}
$controller = $route['controller'];
if (!class_exists($controller)) {
throw new Exception('We couldn't find controller '' . $controller . ''', 1);
}
$class = new $controller(); // grab from di cache when we have one
$method = $route['method'];
if (!method_exists($class, $method)) {
throw new Exception('We couldn't find method '' . $method . ' in controller '' . $controller . ''', 1);
}
$class->$method();
}
}
answered 13 hours ago
Dan
433311
433311
add a comment |
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208658%2fsimple-router-in-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
_SERVER should be provided from outside. What if action shoul accept parameters?
– Vladimir Vukanac
yesterday