Skip to content

Commit

Permalink
Get url_helper functions working with basePath correctly. Fixes #155
Browse files Browse the repository at this point in the history
  • Loading branch information
lonnieezell committed Jul 12, 2016
1 parent 32606a8 commit dc9aa14
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 19 deletions.
21 changes: 20 additions & 1 deletion system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,26 @@ protected function tryToRouteIt(RouteCollectionInterface $routes = null)
// $routes is defined in Config/Routes.php
$this->router = Services::router($routes);

$path = is_cli() ? $this->request->getPath() : $this->request->uri->getPath();
if (is_cli())
{
$path = $this->request->getPath();
}
else
{
$path = $this->request->uri->getPath();

// For web requests, we need to remove the path
// portion of the baseURL, if set, otherwise
// route portions won't be discovered correctly.
if (! empty($this->config->baseURL))
{
$basePath = parse_url($this->config->baseURL, PHP_URL_PATH);
$path = strpos($path, $basePath) === 0
? substr($path, strlen($basePath) -1)
: $path;
}
}


$this->benchmark->stop('bootstrap');
$this->benchmark->start('routing');
Expand Down
15 changes: 10 additions & 5 deletions system/HTTP/IncomingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ class IncomingRequest extends Request

/**
* File collection
*
*
* @var Files\FileCollection
*/
protected $files;

/**
* Negotiator
*
*
* @var \CodeIgniter\HTTP\Negotiate
*/
protected $negotiate;
Expand All @@ -101,7 +101,7 @@ class IncomingRequest extends Request

/**
* Constructor
*
*
* @param type $config
* @param type $uri
* @param type $body
Expand Down Expand Up @@ -177,7 +177,7 @@ public function isSecure(): bool
}

//--------------------------------------------------------------------

/**
* Fetch an item from the $_REQUEST object. This is the simplest way
* to grab data from the request object and can be used in lieu of the
Expand Down Expand Up @@ -370,9 +370,14 @@ protected function detectURI($protocol, $baseURL)
// set our current domain name, scheme
if ( ! empty($baseURL))
{
// We cannot add the path here, otherwise it's possible
// that the routing will not work correctly if we are
// within a sub-folder scheme. So it's modified in
// the
$this->uri->setScheme(parse_url($baseURL, PHP_URL_SCHEME));
$this->uri->setHost(parse_url($baseURL, PHP_URL_HOST));
$this->uri->setPort(parse_url($baseURL, PHP_URL_PORT));
$this->uri->setPath(parse_url($baseURL, PHP_URL_PATH));
}
else
{
Expand Down Expand Up @@ -423,7 +428,7 @@ public function detectPath($protocol)
: $this->parseRequestURI();
break;
}

return $path;
}

Expand Down
25 changes: 12 additions & 13 deletions system/Helpers/url_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
{
/**
* Return a site URL to use in views
*
*
* @param string $path
* @param string|null $scheme
*
Expand All @@ -51,17 +51,15 @@ function site_url(string $path = '', string $scheme = null): string
{
$config = new \Config\App();

$url = ! empty($config->baseURL)
? new \CodeIgniter\HTTP\URI($config->baseURL)
: clone(\CodeIgniter\Services::request()->uri);
$base = base_url();

// Add index page
if (! empty($config->indexPage))
{
$path = rtrim($config->indexPage, '/').'/'.$path;
$path = rtrim($base, '/').'/'.rtrim($config->indexPage, '/').'/'.$path;
}

$url->setPath($path);
$url = new \CodeIgniter\HTTP\URI($path);

if (! empty($scheme))
{
Expand All @@ -78,20 +76,19 @@ function site_url(string $path = '', string $scheme = null): string
{
/**
* Return the base URL to use in views
*
*
* @param string $path
* @param string $scheme
* @return string
*/
function base_url(string $path = '', string $scheme = null): string
{
$config = new \Config\App();

$url = ! empty($config->baseURL)
? new \CodeIgniter\HTTP\URI($config->baseURL)
: clone(\CodeIgniter\Services::request()->uri);
$url = \CodeIgniter\Services::request()->uri;

$url->setPath($path);
if (! empty($path))
{
$url = $url->resolveRelativeURI($path);
}

if (! empty($scheme))
{
Expand Down Expand Up @@ -156,3 +153,5 @@ function index_page()
return $config->indexPage;
}
}

//--------------------------------------------------------------------
143 changes: 143 additions & 0 deletions tests/system/Helpers/URLHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php namespace CodeIgniter\Helpers;

use CodeIgniter\HTTP\URI;
use Config\App;
use CodeIgniter\Services;

class URLHelperTest extends \CIUnitTestCase
{
public function __construct()
{
parent::__construct();

helper('url');
}

//--------------------------------------------------------------------

public function testCurrentURLReturnsBasicURL()
{
$_SERVER['HTTP_HOST'] = 'example.com';
$_SERVER['REQUEST_URI'] = '/';

$this->assertEquals('http://example.com/', current_url());
}

//--------------------------------------------------------------------

public function testCurrentURLReturnsObject()
{
$_SERVER['HTTP_HOST'] = 'example.com';
$_SERVER['REQUEST_URI'] = '/';

$url = current_url(true);

$this->assertTrue($url instanceof URI);
$this->assertEquals('http://example.com/', (string)$url);
}

//--------------------------------------------------------------------

public function testBaseURLBasics()
{
$_SERVER['HTTP_HOST'] = 'example.com';
$_SERVER['REQUEST_URI'] = '/';

$this->assertEquals('http://example.com/', base_url());
}

//--------------------------------------------------------------------

public function testBaseURLAttachesPath()
{
$_SERVER['HTTP_HOST'] = 'example.com';
$_SERVER['REQUEST_URI'] = '/';

$this->assertEquals('http://example.com/foo', base_url('foo'));
}

//--------------------------------------------------------------------

public function testBaseURLAttachesScheme()
{
$_SERVER['HTTP_HOST'] = 'example.com';
$_SERVER['REQUEST_URI'] = '/';

$this->assertEquals('https://example.com/foo', base_url('foo', 'https'));
}

//--------------------------------------------------------------------

public function testBaseURLHeedsBaseURL()
{
$_SERVER['HTTP_HOST'] = 'example.com';
$_SERVER['REQUEST_URI'] = '/';

// Since we're on a CLI, we must provide our own URI
$config = new App();
$config->baseURL = 'http://example.com/public';
$request = Services::request($config);
$request->uri = new URI('http://example.com/public');

Services::injectMock('request', $request);

$this->assertEquals('http://example.com/public', base_url());
}

//--------------------------------------------------------------------

public function testSiteURLBasics()
{
$_SERVER['HTTP_HOST'] = 'example.com';
$_SERVER['REQUEST_URI'] = '/';

$config = new App();
$config->baseURL = '';
$config->indexPage = 'index.php';
$request = Services::request($config);
$request->uri = new URI('http://example.com/');

Services::injectMock('request', $request);

$this->assertEquals('http://example.com/index.php/', site_url());
}

//--------------------------------------------------------------------

public function testSiteURLAttachesPath()
{
$_SERVER['HTTP_HOST'] = 'example.com';
$_SERVER['REQUEST_URI'] = '/';

$config = new App();
$config->baseURL = '';
$config->indexPage = 'index.php';
$request = Services::request($config);
$request->uri = new URI('http://example.com/');

Services::injectMock('request', $request);

$this->assertEquals('http://example.com/index.php/foo', site_url('foo'));
}

//--------------------------------------------------------------------

public function testSiteURLAttachesScheme()
{
$_SERVER['HTTP_HOST'] = 'example.com';
$_SERVER['REQUEST_URI'] = '/';

$config = new App();
$config->baseURL = '';
$config->indexPage = 'index.php';
$request = Services::request($config);
$request->uri = new URI('http://example.com/');

Services::injectMock('request', $request);

$this->assertEquals('ftp://example.com/index.php/foo', site_url('foo', 'ftp'));
}

//--------------------------------------------------------------------

}

0 comments on commit dc9aa14

Please sign in to comment.