Skip to content
This repository was archived by the owner on Nov 25, 2022. It is now read-only.

Commit f05243b

Browse files
committed
We no longer need to override CI site_url function, we made the KB_Config class guess named routes.
1 parent b167719 commit f05243b

File tree

3 files changed

+72
-42
lines changed

3 files changed

+72
-42
lines changed

skeleton/core/KB_Config.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
* @copyright Copyright (c) 2018, Kader Bouyakoub <bkader@mail.com>
3434
* @license http://opensource.org/licenses/MIT MIT License
3535
* @link https://goo.gl/wGXHO9
36-
* @since Version 1.0.0
36+
* @since 1.0.0
3737
*/
3838
defined('BASEPATH') OR exit('No direct script access allowed');
3939

@@ -49,8 +49,8 @@
4949
* @author Kader Bouyakoub <bkader@mail.com>
5050
* @link https://goo.gl/wGXHO9
5151
* @copyright Copyright (c) 2018, Kader Bouyakoub (https://goo.gl/wGXHO9)
52-
* @since Version 1.0.0
53-
* @version 1.0.0
52+
* @since 1.0.0
53+
* @version 2.1.1
5454
*/
5555
class KB_Config extends CI_Config
5656
{
@@ -90,6 +90,30 @@ public function set_item($item, $value = null, $index = '')
9090

9191
// ------------------------------------------------------------------------
9292

93+
/**
94+
* Builds a URI string.
95+
*
96+
* This method is called before parent's in order to use our named routes
97+
* system.
98+
*
99+
* @access protected
100+
* @param mixed $uri URI string or an array of segments.
101+
* @return string
102+
*/
103+
protected function _uri_string($uri)
104+
{
105+
if (class_exists('Route', false))
106+
{
107+
$uri = is_array($uri)
108+
? array_map(array('Route', 'named'), $uri)
109+
: Route::named($uri);
110+
}
111+
112+
return parent::_uri_string($uri);
113+
}
114+
115+
// ------------------------------------------------------------------------
116+
93117
/**
94118
* Returns the name or details about the language currently in use.
95119
* @access public

skeleton/helpers/KB_url_helper.php

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -49,45 +49,9 @@
4949
* @link https://goo.gl/wGXHO9
5050
* @copyright Copyright (c) 2018, Kader Bouyakoub (https://goo.gl/wGXHO9)
5151
* @since 1.0.0
52-
* @version 2.0.1
52+
* @version 2.1.1
5353
*/
5454

55-
if ( ! function_exists('site_url'))
56-
{
57-
/**
58-
* site_url
59-
*
60-
* We override CodeIgniter default function behavior in order to use
61-
* the named routes feature.
62-
*
63-
* @author Kader Bouyakoub
64-
* @link https://goo.gl/wGXHO9
65-
* @since 2.0.0
66-
*
67-
* @param string $name
68-
* @param string $protocol
69-
* @return string
70-
*/
71-
function site_url($name = '', $protocol = NULL)
72-
{
73-
// Keep $_GET parameters if we have any.
74-
$get = null;
75-
// If the route is not found, we use $name.
76-
if (false !== ($position = strpos($name, '?')))
77-
{
78-
$get = substr($name, $position);
79-
$name = strtok($name, '?');
80-
}
81-
82-
// Attempt to get the named route first.
83-
$uri = Route::named($name);
84-
(null === $uri) && $uri = $name;
85-
return get_instance()->config->site_url($uri.$get, $protocol);
86-
}
87-
}
88-
89-
// ------------------------------------------------------------------------
90-
9155
if ( ! function_exists('anchor'))
9256
{
9357
/**

skeleton/third_party/bkader/class-route.php

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
* @link https://goo.gl/wGXHO9
5050
* @copyright Copyright (c) 2018, Kader Bouyakoub (https://goo.gl/wGXHO9)
5151
* @since 1.0.0
52-
* @version 1.0.0
52+
* @version 2.1.0
5353
*
5454
* Original author:
5555
* @author Patroklo
@@ -535,7 +535,33 @@ public static function get_prefix()
535535
*/
536536
public static function named($name)
537537
{
538-
return (isset(self::$named_routes[$name])) ? self::$named_routes[$name] : null;
538+
// Make the method remember cached routes.
539+
static $cached = array();
540+
541+
if ( ! isset($cached[$name]))
542+
{
543+
// Store the raw name and attempt to keep $_GET parameters.
544+
$raw_name = $name;
545+
$get = null;
546+
547+
// In case there are GET parameters, update both $raw_name and $get.
548+
if (false !== ($pos = strpos($raw_name, '?')))
549+
{
550+
$get = substr($raw_name, $pos);
551+
$raw_name = strtok($raw_name, '?');
552+
}
553+
554+
// Use the named route if found.
555+
if (isset(self::$named_routes[$raw_name]))
556+
{
557+
$raw_name = self::$named_routes[$raw_name];
558+
}
559+
560+
// Cache the route before returning it.
561+
$cached[$name] = $raw_name.$get;
562+
}
563+
564+
return $cached[$name];
539565
}
540566

541567
// ------------------------------------------------------------------------
@@ -636,3 +662,19 @@ private static function create_route($from, $to, $options = array(), $nested = f
636662
}
637663

638664
}
665+
666+
// ------------------------------------------------------------------------
667+
668+
if ( ! function_exists('route'))
669+
{
670+
/**
671+
* Return a route from routes array using the name is was defined with.
672+
* @see Route::named
673+
* @param string $name The route's name.
674+
* @return string
675+
*/
676+
function route($name)
677+
{
678+
return Route::named($name);
679+
}
680+
}

0 commit comments

Comments
 (0)