Skip to content

Instantly share code, notes, and snippets.

@meddulla
Created May 12, 2011 23:08
Show Gist options
  • Select an option

  • Save meddulla/969648 to your computer and use it in GitHub Desktop.

Select an option

Save meddulla/969648 to your computer and use it in GitHub Desktop.
restfull output urls for code igniter
<IfModule mod_rewrite.c>
RewriteEngine On
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#This last condition enables access to the images and css folders, and the robots.txt file
#Submitted by Michael Radlmaier (mradlmaier)
RewriteCond $1 !^(index\.php|public|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
<?php
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
$config['url_suffix'] = '';//required
<?php
//eg welcome/index
$route['([a-z]+)/([a-z]+)'] = "$1/$2";
//eg welcome/index.json
$route['([a-z]+)/([a-z]+).([a-z]+)'] = "$1/$2";
?>
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
$this->load->helper('url');
echo 'URL: ' . current_url().'<br/>';
echo 'FORMAT: ' . $this->_format().'<br/>';
echo 'Building a url: ' . site_url("welcome/local/123.html").'<br/>';
echo 'INTERESTING OUTPUTS: .json, .xml, .html, .cache <br/>';
echo 'ACTIONS: .clear (->an action to clear cache)';
}
//this should be moved to a helper function
//or a MY_Controller should be created
//but it shows how this can be done
//could also be done in MY_Model (a la rails)
//as long as cache control is also there
private function _format()
{
$current_url = current_url();
$extension = substr(strrchr($current_url, '.'), 1);
if (empty($extension)) {
return 'html';//default extension
}
return $extension;
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
@meddulla

Copy link
Copy Markdown
Author

main logic in routes.php & welcome.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment