Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

[solved] Setting baseURI automatically for any environment

Hi.

In my case, a path from DocumentRoot is different from Local in Web.

So, I set baseURI.

<?php
# /public/index.php

$di->set('url', function(){
    $url = new Phalcon\Mvc\Url();
    if (preg_match('/bar/', __DIR__)) {
        $url->setBaseUri('/bar/');
    }
    return $url;
});

It is not flexable.
I have to hard-code a path.
Is there any other way?

Thanks.



82.3k
Accepted
answer
edited Aug '14

You can set it in the configuration file:

$di->set('url', function() use ($config) {
    $url = new Phalcon\Mvc\Url();    
    $url->setBaseUri($config->application->baseUri);    
    return $url;
});
edited Mar '14

If you have multiple environments, it's better to do it this way

in your config.php

<?php
return new \Phalcon\Config(array(
    'environment' => 'development'
));

Then what you can do in the Url service

<?php
$config = require '/path/to/config.php';

$di->set('url', function () use ($config) {
    $url = new \Phalcon\Mvc\Url();

    switch ($config->environment) {
        case 'development':
            $baseUri = '/dev/';
            break;
        case 'production':
            $baseUri = '/prod/';
            break;
        default:
            // Just for insurance
            throw new \InvalidArgumentException('The config environment is not set.');
    }

    $url->setBaseUri($baseUri);

    return $url;
});

Hope this helps....

Hmm... There is no way fitting the difference in baseURL automatically, isn't it ? All right. I devise it for me.

Thaks a lot.



3.3k
edited Mar '15

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,
    )
));