讓CodeIgniter 直接支援RestFull 的修改方法

來源:互聯網
上載者:User

標籤:codeigniter   route   restfull   

修改檔案 /system/core/Router.php 的方法 _parse_route()

/** *  Parse Routes * * This function matches any routes that may exist in * the config/routes.php file against the URI to * determine if the class/method need to be remapped. * * @accessprivate * @returnvoid */function _parse_routes(){// Turn the segment array into a URI string$uri = implode('/', $this->uri->segments);// Is there a literal match?  If so we're doneif (isset($this->routes[$uri])){return $this->_set_request(explode('/', $this->routes[$uri]));}// Loop through the route array looking for wild-cardsforeach ($this->routes as $key => $val){// Convert wild-cards to RegEx$key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));// Does the RegEx match?if (preg_match('#^'.$key.'$#', $uri)){// Do we have a back-reference?if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE){$val = preg_replace('#^'.$key.'$#', $val, $uri);}return $this->_set_request(explode('/', $val));}}// If we got this far it means we didn't encounter a// matching route so we'll set the site default route$this->_set_request($this->uri->segments);}

使用方法:修改路由檔案 /application/config/routes.php

$routes['index'] = array('get'=>'news/lst','post'=>'news/lst','put'=>'news/lst',);

by default7#zbphp.com 

特別備忘,從官網和中文CI網站下載來的CI 2.2 /2.1 包括從github官網下載來的CI,都是沒有這一段的。



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.