Skip to main content
added 11 characters in body; edited tags; edited title
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

Does this code contain any useless parts? Dispatcher for a JSON API without routes

Does this code contain any useless parts? ImI am using itthis code for a fast JSON API without routes. Is

Does this code contain any useless parts? Is the class loader efficient enough? Are there any better options? Is ReflectionMethodReflectionMethod slow if used once?

<?php

/*
 * Init stuff
 */

session_start();

define('ROOT', realpath( dirname(__FILE__) ) . '/');
set_include_path(ROOT . 'src/');

spl_autoload_extensions('.php');
spl_autoload_register();

ini_set('error_reporting', E_ALL & ~E_DEPRECATED);
ini_set('display_errors',  1); //To change in prod to 0 <<<<<<<<<<<<<<<<<<<<<<<<<
ini_set('log_errors',      1);
ini_set('error_log',       ROOT . 'tmp/logs/application.log');

date_default_timezone_set('Europe/Paris');

/*
 * Process stuff
 */

$params      = explode( '/', trim( key($_GET), '/' ) );
$paramsCount = count($params);

if ($paramsCount >= 2) {
  try {
    $controllerMethod = new ReflectionMethod( '\app\controller\\' .
                                                  $params[1], $params[2]
                                                );

    if ($paramsCount - 2 >= $controllerMethod->getNumberOfRequiredParameters()) {

      \ens\controller\Request::$lang =& $params[0];
      \ens\controller\Request::$data =& $_POST;

      $response = $controllerMethod->invokeArgs( null, array_splice($params, 3) );

    } else {
      $response = array('status' => 400);
    }

  } catch (ReflectionException $e) {
    $response = array('status' => 300);
  }

} else {
  $response = array('status' => 301);
}

if (!headers_sent()) {
  header('Content-Type: application/json');
}

echo json_encode($response);

Does this code contain any useless parts?

Does this code contain any useless parts? Im am using it for a fast JSON API without routes. Is the class loader efficient enough? Are there any better options? Is ReflectionMethod slow if used once?

<?php

/*
 * Init stuff
 */

session_start();

define('ROOT', realpath( dirname(__FILE__) ) . '/');
set_include_path(ROOT . 'src/');

spl_autoload_extensions('.php');
spl_autoload_register();

ini_set('error_reporting', E_ALL & ~E_DEPRECATED);
ini_set('display_errors',  1); //To change in prod to 0 <<<<<<<<<<<<<<<<<<<<<<<<<
ini_set('log_errors',      1);
ini_set('error_log',       ROOT . 'tmp/logs/application.log');

date_default_timezone_set('Europe/Paris');

/*
 * Process stuff
 */

$params      = explode( '/', trim( key($_GET), '/' ) );
$paramsCount = count($params);

if ($paramsCount >= 2) {
  try {
    $controllerMethod = new ReflectionMethod( '\app\controller\\' .
                                                  $params[1], $params[2]
                                                );

    if ($paramsCount - 2 >= $controllerMethod->getNumberOfRequiredParameters()) {

      \ens\controller\Request::$lang =& $params[0];
      \ens\controller\Request::$data =& $_POST;

      $response = $controllerMethod->invokeArgs( null, array_splice($params, 3) );

    } else {
      $response = array('status' => 400);
    }

  } catch (ReflectionException $e) {
    $response = array('status' => 300);
  }

} else {
  $response = array('status' => 301);
}

if (!headers_sent()) {
  header('Content-Type: application/json');
}

echo json_encode($response);

Dispatcher for a JSON API without routes

I am using this code for a fast JSON API without routes.

Does this code contain any useless parts? Is the class loader efficient enough? Are there any better options? Is ReflectionMethod slow if used once?

<?php

/*
 * Init stuff
 */

session_start();

define('ROOT', realpath( dirname(__FILE__) ) . '/');
set_include_path(ROOT . 'src/');

spl_autoload_extensions('.php');
spl_autoload_register();

ini_set('error_reporting', E_ALL & ~E_DEPRECATED);
ini_set('display_errors',  1); //To change in prod to 0 <<<<<<<<<<<<<<<<<<<<<<<<<
ini_set('log_errors',      1);
ini_set('error_log',       ROOT . 'tmp/logs/application.log');

date_default_timezone_set('Europe/Paris');

/*
 * Process stuff
 */

$params      = explode( '/', trim( key($_GET), '/' ) );
$paramsCount = count($params);

if ($paramsCount >= 2) {
  try {
    $controllerMethod = new ReflectionMethod( '\app\controller\\' .
                                                  $params[1], $params[2]
                                                );

    if ($paramsCount - 2 >= $controllerMethod->getNumberOfRequiredParameters()) {

      \ens\controller\Request::$lang =& $params[0];
      \ens\controller\Request::$data =& $_POST;

      $response = $controllerMethod->invokeArgs( null, array_splice($params, 3) );

    } else {
      $response = array('status' => 400);
    }

  } catch (ReflectionException $e) {
    $response = array('status' => 300);
  }

} else {
  $response = array('status' => 301);
}

if (!headers_sent()) {
  header('Content-Type: application/json');
}

echo json_encode($response);
Tweeted twitter.com/#!/StackCodeReview/status/161999028454817792

Does this code contain any useless parts? Im am using it for a fast JSON API without routes. Is the class loader efficient enough? Are there any better options? Is ReflectionMethod slow if used once?

<?php

/*
 * Init stuff
 */

session_start(); 

define('ROOT', realpath( dirname(__FILE__) ) . '/');
set_include_path(ROOT . 'src/'); 

spl_autoload_extensions('.php');
spl_autoload_register(); 

ini_set('error_reporting', E_ALL & ~E_DEPRECATED);
ini_set('display_errors',  1); //To change in prod to 0 <<<<<<<<<<<<<<<<<<<<<<<<<
ini_set('log_errors',      1);
ini_set('error_log',       ROOT . 'tmp/logs/application.log'); 

date_default_timezone_set('Europe/Paris');

/*
 * Process stuff
 */

$params      = explode( '/', trim( key($_GET), '/' ) );
$paramsCount = count($params); 

if ($paramsCount >= 2) {
  try {
    $controllerMethod = new ReflectionMethod( '\app\controller\\' .
                                                  $params[1], $params[2]
                                                ); 

    if ($paramsCount - 2 >= $controllerMethod->getNumberOfRequiredParameters()) { 

      \ens\controller\Request::$lang =& $params[0];
      \ens\controller\Request::$data =& $_POST; 

      $response = $controllerMethod->invokeArgs( null, array_splice($params, 3) ); 

    } else {
      $response = array('status' => 400);
    } 

  } catch (ReflectionException $e) {
    $response = array('status' => 300);
  } 

} else {
  $response = array('status' => 301);
} 

if (!headers_sent()) {
  header('Content-Type: application/json');
} 

echo json_encode($response);

Does this code contain any useless parts? Im am using it for a fast JSON API without routes. Is the class loader efficient enough? Are there any better options? Is ReflectionMethod slow if used once?

<?php

/*
 * Init stuff
 */

session_start();
define('ROOT', realpath(dirname(__FILE__)) . '/');
set_include_path(ROOT . 'src/');
spl_autoload_extensions('.php');
spl_autoload_register();
ini_set('error_reporting', E_ALL & ~E_DEPRECATED);
ini_set('display_errors', 1); //To change in prod to 0 <<<<<<<<<<<<<<<<<<<<<<<<<
ini_set('log_errors', 1);
ini_set('error_log', ROOT . 'tmp/logs/application.log');
date_default_timezone_set('Europe/Paris');

/*
 * Process stuff
 */

$params = explode('/', trim(key($_GET), '/'));
$paramsCount = count($params);
if ($paramsCount >= 2) {
  try {
    $controllerMethod = new ReflectionMethod('\app\controller\\' . $params[1], $params[2]);
    if ($paramsCount - 2 >= $controllerMethod->getNumberOfRequiredParameters()) {
      \ens\controller\Request::$lang =& $params[0];
      \ens\controller\Request::$data =& $_POST;
      $response = $controllerMethod->invokeArgs(null, array_splice($params, 3));
    } else {
      $response = array('status' => 400);
    }
  } catch (ReflectionException $e) {
    $response = array('status' => 300);
  }
} else {
  $response = array('status' => 301);
}
if (!headers_sent()) {
  header('Content-Type: application/json');
}
echo json_encode($response);

Does this code contain any useless parts? Im am using it for a fast JSON API without routes. Is the class loader efficient enough? Are there any better options? Is ReflectionMethod slow if used once?

<?php

/*
 * Init stuff
 */

session_start(); 

define('ROOT', realpath( dirname(__FILE__) ) . '/');
set_include_path(ROOT . 'src/'); 

spl_autoload_extensions('.php');
spl_autoload_register(); 

ini_set('error_reporting', E_ALL & ~E_DEPRECATED);
ini_set('display_errors',  1); //To change in prod to 0 <<<<<<<<<<<<<<<<<<<<<<<<<
ini_set('log_errors',      1);
ini_set('error_log',       ROOT . 'tmp/logs/application.log'); 

date_default_timezone_set('Europe/Paris');

/*
 * Process stuff
 */

$params      = explode( '/', trim( key($_GET), '/' ) );
$paramsCount = count($params); 

if ($paramsCount >= 2) {
  try {
    $controllerMethod = new ReflectionMethod( '\app\controller\\' .
                                                  $params[1], $params[2]
                                                ); 

    if ($paramsCount - 2 >= $controllerMethod->getNumberOfRequiredParameters()) { 

      \ens\controller\Request::$lang =& $params[0];
      \ens\controller\Request::$data =& $_POST; 

      $response = $controllerMethod->invokeArgs( null, array_splice($params, 3) ); 

    } else {
      $response = array('status' => 400);
    } 

  } catch (ReflectionException $e) {
    $response = array('status' => 300);
  } 

} else {
  $response = array('status' => 301);
} 

if (!headers_sent()) {
  header('Content-Type: application/json');
} 

echo json_encode($response);
Source Link

Does this code contain any useless parts?

Does this code contain any useless parts? Im am using it for a fast JSON API without routes. Is the class loader efficient enough? Are there any better options? Is ReflectionMethod slow if used once?

<?php

/*
 * Init stuff
 */

session_start();
define('ROOT', realpath(dirname(__FILE__)) . '/');
set_include_path(ROOT . 'src/');
spl_autoload_extensions('.php');
spl_autoload_register();
ini_set('error_reporting', E_ALL & ~E_DEPRECATED);
ini_set('display_errors', 1); //To change in prod to 0 <<<<<<<<<<<<<<<<<<<<<<<<<
ini_set('log_errors', 1);
ini_set('error_log', ROOT . 'tmp/logs/application.log');
date_default_timezone_set('Europe/Paris');

/*
 * Process stuff
 */

$params = explode('/', trim(key($_GET), '/'));
$paramsCount = count($params);
if ($paramsCount >= 2) {
  try {
    $controllerMethod = new ReflectionMethod('\app\controller\\' . $params[1], $params[2]);
    if ($paramsCount - 2 >= $controllerMethod->getNumberOfRequiredParameters()) {
      \ens\controller\Request::$lang =& $params[0];
      \ens\controller\Request::$data =& $_POST;
      $response = $controllerMethod->invokeArgs(null, array_splice($params, 3));
    } else {
      $response = array('status' => 400);
    }
  } catch (ReflectionException $e) {
    $response = array('status' => 300);
  }
} else {
  $response = array('status' => 301);
}
if (!headers_sent()) {
  header('Content-Type: application/json');
}
echo json_encode($response);