custom URL mappings in codeigniter like redmine? - php

So redmine has a very peculiar url mapping style that i observed :
http://demo.redmine.org/projects/<project-name>/controller/action
samples :
http://demo.redmine.org/projects/produto/activity
http://demo.redmine.org/projects/produto/issues/new
http://demo.redmine.org/projects/produto/issues/gantt
http://demo.redmine.org/projects/produto/files
and the url changes as the project changes.
how do i do this in codeigniter ? I'm thinking it can be done with routes.php but so far i'm not able to get anywhere.
Looking for any help. Thanks.

Use the following function inside your "application/controllers/projects.php" controller:
public function _remap($method)
{
if ($method == 'project-name')
{
//display project1
}
elseif($method == 'project-name2')
{
//display project2
}
}
You can do the same for varying methods by extracting them from database
take a look here:
http://codeigniter.com/user_guide/general/controllers.html#remapping
you can also route your controller by using custom routes in application/config/routes.php
$route['example'] = "controller/function";
$route['example2/(:any)'] = "controller/function";

You use the routes file in application/config/routes.php
You would use something like this:
// the $1 maps to :any
$route['projects/produto/:any'] = "$1";
// the $1 maps to the first any, $2 maps to the second :any
$route['projects/produto/:any/:any'] = "$1/$2";
You will want mod_rewrite enabled if you are handling clean URL's. Otherwise expect the index.php/controller/action. I cant test it myself there, but you should refer to:
Once you add a route (It has to be called $route[] inside the configuration), refresh the page and try to go to the URL!
http://codeigniter.com/user_guide/general/routing.html

add this to your routes.php (btw: you need url-rewriting enabled for routes to work, ie. using .htaccess)
$route['projects/(:any)/(:any)/(:any)'] = "$2/$3/$1";
for example /projects/produto/issues/new will call the function new in the class issues and pass it the parameter 'produto'
also check http://codeigniter.com/user_guide/general/routing.html

Related

Codeigniter use extra url parameter

In codeigniter I want to have one url segment to not be consider.
example.com/app/dev1/controller/function/id
example.com/app/dev2/controller/function/id
base url is
baseUrl = example.com/app
When I use this type of url, codeigniter consider "dev1" as controller, "controller" as function, so I get error of page not found.
I want to know if I can code which don't consider first parameter as controller and it start considering from second parameters.
I can not add in base url as it is not constant we can have en, fr, nl etc so do we have anything that help in this case I don't want to add query string "?".
Can we do anything using .htaccess
suppose url is like following : www.paintes.com/painters-in-chennai
then route can be like following
$route['painters-in-(:any)'] ='index/paintersIn/$1';
in index.php controller, i'll be able to receive chennai like following
function paintesIn($city_name)
{
echo $city_name //OUTPUT WILL BE "chennai"
}
if url is like www.paintes.com/painters-in-mumbai , then output will be mumbai
as per your request, URL is liek following : www.paintes.com/mumbai-painters
then you can write like following
$route['(:any)-painters'] ='index/paintersIn/$1';
You have to configure your CodeIgniter routes properly. Example:
$route['dev1/controller/fun/(:num)'] = "dev1/controller/fun/index/$1";
See https://blog.biernacki.ca/2011/12/codeigniter-uri-routing-issue-with-controller-folders/
You might want to go for the following in routing
$route['dev1/(:any)'] ='dev1/$1';
$route['dev1/(:any)/(:any)'] ='dev1/$1/$2';
$route['dev1/(:any)/(:any)/(:any)'] ='dev1/$1/$2/$3';
Its rare that Codeigniter treats folder name as controller, try renaming folderas well.
Otherwise above solution should work.
You could add a URI Routing regular expression like this to skip the dev part and go straight to the desired controller :
$route['dev(:num)/([a-z]+)/([a-z]+)/(\d+)'] = '$2/$3/$4'; // rule to match method with parameter
$route['dev(:num)/([a-z]+)/([a-z]+)'] = '$2/$3'; // rule to match method without parameter
Or if you don have a common dev URI segment, you could use :any rule :
$route['(:any)/([a-z]+)/([a-z]+)/(\d+)'] = '$2/$3/$4'; // rule to match method with parameter
$route['(:any)/([a-z]+)/([a-z]+)'] = '$2/$3'; // rule to match method without parameter
we will neeed to create new file "MY_Router.php" in "/application/core" folder, with following content
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Router extends CI_Router {
protected function _parse_routes()
{
// do logic you needed
unset($this->uri->segments[1]);
// Return default function
return parent::_parse_routes();
}
}

Purpose of routes.php in codeigniter

I just want to know the purpose of this on routes.php
$route['default_controller'] = "welcome";
$route['scaffolding_trigger'] = "";
Let me first take your through routing and then tell you about the meaning of those lines in routes.php.
You should start with URI Routing. In short, what you do with routes is that you map a certain URI with a controller/method/parameter statement.
Examples
See the following examples taken from the user guide:
So, something like example.com/journals can be routed to the blogs controller.
$route['journals'] = "blogs";
Another good example is when you are building a product catalogue and you need example.com/product/some_id to be routed to a controller catalog:
$route['product/(:num)'] = "catalog/product_lookup_by_id/$1";
In the example above, catalog will be the controller, product_lookup_by_id will be the method and $1 is the parameter which is picked up from the URI.
Answer to your question
You have asked:
I just want to know the purpose of this on routes.php
$route['default_controller'] = "welcome";
$route['scaffolding_trigger'] = "";
The default_controller is quite obvious. This means welcome/index will be loading whenever `example.com/index is being requested.
scaffolding_trigger was deprecated in 1.7 but you can read about it. Scaffolding was a method which you could use to seed data in your database.
$route['default_controller'] = "welcome";
This is the default controller which codeigniter would start with when you didnt specify a controller to the URL.
url:
ip/monitor/index.php
This will trigger the default controller aka welcome.php
url:
ip/monitor/index.php/controller
This will, however, trigger your given controller and not the default one
This is mostly used to asign a index page as the start page.
I'm not sure about $route['scaffolding_trigger'] = ""; as i never used it. But according to the comments it has been removed in version 2.0
Routing rules are defined in your application/config/routes.php file. In it you'll see an array called $route that permits you to specify your own routing criteria. Routes can either be specified using wildcards or Regular Expressions.
For more detail please see this link :
http://ellislab.com/codeigniter%20/user-guide/general/routing.html

Remapping Codeigniter Controller

I've been playing with Codeigniter lately, and I came to know that you can remap a function inside a Controller so that you can have dynamic pretty URL.
I just want to know can the same be done with controllers? I mean If I call http://example.com/stack, it'll look for a controller named stack and if not found, it'll call a fixed/remapped controller where I can take care of it.
Can this be done?
Maybe this can help you:
function _remap( $method )
{
/// $method contains the second segment of your URI
switch( $method )
{
case 'about-me':
$this->about_me();
break;
case 'successful':
$this->display_successful_message();
break;
default:
$this->page_not_found();
break;
}
}
Yes, it can be done, you achieve it by using uri routing.
In your application/config/routes.php you can set your custom routes to remapping URIs.
There are already 2 provided, the default one (in case no controller is called) and the 404 error routing.
Now, if you want to add custom routes, you just add them UNDER those 2 defaults, keeping in mind that they're executed in the order they're presented.
Say, for example, you want to remap 'stack' to another controller, just use:
$routes['stack'] = 'othercontroller';
In this way, whenever you access 'stack' it will be automatically mapped to 'othercontroller', and if that doesn't exists..well, you get the same 404 error.
If you're hiding index.php from the URL with .htaccess remember to insert it into the $config['index_page'] = 'index.php';.
If what you're trying to achieve, instead, is a custom error message when a controller is not found, just override the 404 route as already suggested by #Juris Malinens, using your custom default controller to handle that situation
$route['404_override'] = 'customcontroller';
You can use .htaccess to do this or config/routes.php- Codeigniter is very flexible ;-)
If controller is not found use $route['404_override']; in config/routes.php
The application/config/routes.php file would be the appropriate place to do this. The 404_override mentioned by Juris is only available in CI 2.x just in case you have an older version (I don't know, you may be working on a legacy system, or may have to in the future).
Note, you can do more than just "remap" controllers with this. The routes accept regex patterns like htaccess rewrite rules; there are also some CI patterns which are basically just more human readable alias for regexes. Say you had an Articles controller with category, search and article functions, you might have routes that looked like:
$route["category/(:any)"] = "articles/category/$1";
$route["search/(:any)"] = "articles/search/$1";
$route["(:any)"] = "articles/article/$1';
You see how you can use routes to completely remove the controller name from you URLs? These rules would fall back to assuming the page is an article if the URL doesn't specifically say it's a category page or a search query. You could then check if you had an article for the URL and display a 404 as appropriate.

Codeigniter first segment URL variables

I am looking to use a URL shortening scheme where I would like the variable to be in the first segment of the URL, www.example.com/0jf08h204. My default controller is "home.php" and I have .htaccess mod-rewrites in place, so what is the best way to manage this? I suppose my smarts have been blocked by the standard /controller/method/variable scheme, is this a URI Protocol setting? Thank you!
To add to Matthew's response.
This is what you'll need in your system/application/config/routes.php file:
$route['(:any)'] = "home";
This will redirect EVERYTHING.
You might not want to redirect everything if you have other controllers which you need to use. If that is the case you can use this regular expression instead:
$route['^(?!about|contact)\S*'] = "home";
This will allow you to redirect everything except the controllers 'about' or 'contact' -- these will be directed to the 'about.php' and 'contact.php' controllers.
Please note, I choose not to use the wild cards within CodeIgniter, you might find they work better for you, I however choose to parse out the $_SERVER['REQUEST_URI'] manually after the redirect. If however you wanted to use the wildcards you would just add /$1 to the routes as you see in Matthew's response.
I myself haven't played a whole lot with Routing, but I think you could try something like this:
$route['(:any)'] = "controllername/actionname/$1";
Haven't tried this myself though.
Well even though it seems a bit non-standard to whitelist certain areas, and I am still running through my head how 404 errors will be handled, here is a method that is working for now using the regex from evolve. My home.php controller file:
class Home extends Controller {
function Home()
{
parent::Controller();
$uri = uri_string();
if(!empty($uri)) {
$uri_array = explode('/',$uri);
$first_segment = $uri_array[1];
if(isset($first_segment)) {
//do stuff, load alternative view
}
}
}
function index()
{
$this->load->view('home_view');
}
}
/* EOF */
Thanks for the help, please post alternate soln's if available.

CodeIgniter htaccess Redirect

I'm hoping that this will be a simple question that someone can answer. I'm looking to build a CodeIgniter application that I can build pretty easily in regular PHP.
Example: I would like to go to http://locahost/gregavola and have rewritten using htaccess file to profile.php?user=gregavola. How can I do this in CodeIgniter?
Usually in htaccess I could write ^(\w+)$ profile.php?user=$1 but that won't work with the paths in CodeIgniter.
Any suggestions?
CodeIgniter turns off GET parameters by default; instead of rewriting the URL to a traditional GET style (IE, with the ?), you should create a user controller and send the request to:
http://localhost/user/info/gregavola
Then in the user controller, add the following stub:
function info($name)
{
echo $name;
}
From here you would probably want to create a view and pass $name into it:
$data['name'] = 'Your title';
$this->load->view('user_info', $data);
You can find all of this in the CodeIgniter User Guide, which is an excellent resource for getting started.
To map localhost/gregavola to a given controller and function, modify the routes file at application/config/routes.php like so:
$route['(:any)'] = "user/info/$1"
Routes are run in the order they are received, so if you have other routes like localhost/application/dosomething/, you will want to include those routes first so that every page in your entire app doesn't become a user page.
Read more about CI routes here: http://codeigniter.com/user_guide/general/routing.html
Good luck!

Categories