shie2’s diary

仕事とかエフェクターとかもろもろのために

【codeigniter】Routerでアクションメソッドに送られる引数をいじった

domain.com/aug1/aug2/aug3

というURLから
domain.com/page/index/aug1/aug2/aug3
で動作させたいので、

<?php
$route['(.+)'] = 'page/index/$1';
?>

こう書いて

<?php
class page extends CI_Controller {

    public function index($pageUrl = 'index')
    {
        var_dump($pageUrl);
    }
}
?>

で表示されるのは

aug1

だった!

ちげーよ!!!

aug1/aug2/aug3

こんな感じに引数が来てほしい!

なんで、
system/core/Router.phpをいじるよ!

application/core/MY_Router.phpを作る。

<?php 
class MY_Router extends CI_Router {

    protected function _parse_routes()
    {
        // Turn the segment array into a URI string
        $uri = implode('/', $this->uri->segments);

        // Get HTTP verb
        $http_verb = isset($_SERVER['REQUEST_METHOD']) ? strtolower($_SERVER['REQUEST_METHOD']) : 'cli';

        // Loop through the route array looking for wildcards
        foreach ($this->routes as $key => $val)
        {
            // Check if route format is using HTTP verbs
            if (is_array($val))
            {
                $val = array_change_key_case($val, CASE_LOWER);
                if (isset($val[$http_verb]))
                {
                    $val = $val[$http_verb];
                }
                else
                {
                    continue;
                }
            }

            // Convert wildcards to RegEx
            $key = str_replace(array(':any', ':num'), array('[^/]+', '[0-9]+'), $key);
            // Does the RegEx match?
            if (preg_match('#^'.$key.'$#', $uri, $matches))
            {
                // Are we using callbacks to process back-references?
                if ( ! is_string($val) && is_callable($val))
                {
                    // Remove the original string from the matches array.
                    array_shift($matches);

                    // Execute the callback using the values in matches as its parameters.
                    $val = call_user_func_array($val, $matches);
                }
                // Are we using the default routing method for back-references?
                elseif (strpos($val, '$') !== FALSE && strpos($key, '(') !== FALSE)
                {
                    $val = preg_replace('#^'.$key.'$#', $val, $uri);
                }
                //20170410 拡張
                //pageコントローラーの時は引数を/で区切らない
                if(preg_match('/^page\/index\/(.*)/',$val,$matches)){
                    $this->_set_request(array('page','index',$matches[1]));
                }else{
                    $this->_set_request(explode('/', $val));
                }
                return;
            }
        }

    }
}

ごっつ一時しのぎ的な感じだけど、
とりあえずこれで

aug1/aug2/aug3

ってかえってきたよ!