Automatic base uri detection.
define these constants on top of your public/index.php
// setup constants
if (!defined('DS')) {
define('DS', DIRECTORY_SEPARATOR);
}
if (!defined('ROOTFULLPATH')) {
define('ROOTFULLPATH', dirname(__DIR__));// if this file is under /public, up one level.
}
if (!defined('APPFULLPATH')) {
define('APPFULLPATH', ROOTFULLPATH.DS.'app');
}
add MyConfig class in app/config/config.php file and call to getBaseUri().
<?php
class MyConfig extends \Phalcon\Config
{
public function getBaseUri()
{
$replaced_slash_fullpath = str_replace('\\', '/', ROOTFULLPATH);
$request_uri_no_slash_trail = rtrim($_SERVER["REQUEST_URI"], '/');
return $this->findMatchUri($replaced_slash_fullpath, $request_uri_no_slash_trail);
}
private function findMatchUri($str1 = '', $str2 = '')
{
if (strpos($str1, '/') === false || strpos($str2, '/') === false) {
return '/';
}
$str1_arr = explode('/', $str1);
$str2_arr = explode('/', $str2);
$found_matched = [];
foreach ($str1_arr as $uri_target) {
foreach ($str2_arr as $uri_find) {
if (strcmp($uri_find, $uri_target) === 0) {
$found_matched[] = $uri_find;
}
}
}
unset($str1_arr, $str2_arr);
if (empty($found_matched)) {
$found_matched_string = '/';
} else {
$found_matched_string = '/'.implode('/', $found_matched).'/';
}
unset($found_matched);
return $found_matched_string;
}// findMatchUri
}
$myconfig = new MyConfig();
$auto_base_uri = $myconfig->getBaseUri();
unset($myconfig);
return new \Phalcon\Config(array(
'database' => array(
'adapter' => 'Mysql',
'host' => 'localhost',
'username' => 'admin',
'password' => 'pass',
'dbname' => 'test',
'charset' => 'utf8',
),
'application' => array(
'controllersDir' => __DIR__ . '/../../app/controllers/',
'modelsDir' => __DIR__ . '/../../app/models/',
'viewsDir' => __DIR__ . '/../../app/views/',
'pluginsDir' => __DIR__ . '/../../app/plugins/',
'libraryDir' => __DIR__ . '/../../app/library/',
'cacheDir' => __DIR__ . '/../../app/cache/',
'baseUri' => $auto_base_uri,
)
));