Viewed   108 times

I just thought of routing my URLs which is like

http://localhost/codeigniter_cup_myth/index.php/adminController/mainAccount

and it would be pretty nice if it was

http://localhost/codeigniter_cup_myth/Accounts/mainAccount

Where should I start when routing URLs in my web application?

 Answers

3

Also, adding something like:

RewriteEngine on
RewriteCond $1 !^(index.php|images|css|js|robots.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

to your .htaccess file will get rid of the index.php part

Monday, August 15, 2022
 
3

Try this code it might help you :

Here dashboard is the name of controller

//this will route as localhost/appFolder/admin/index
  $route['admin'] = 'dashboard'; // for your index page

//this will route as localhost/appFolder/admin/method_name
 $route['admin/(:any)'] = 'dashboard/$1';

//this will route as localhost/appFolder/admin/method_name/param1
$route['admin/(:any)/(:any)'] = 'dashboard/$1/$2';

Link the route Like

// for your index page
<a href="<?php echo base_url('admin/index'); ?>"></a>

// for your other pages
<a href="<?php echo base_url('admin/method_name'); ?>"></a>

To link the other controller defined just like

 <a href="<?php echo base_url('otherControllerName/method_name'); ?>"></a>
Sunday, September 4, 2022
 
5

Well, routing is effecient - the alternative is remapping your controllers.

Let's take a look at both possibilities.

An imaginary situtation: At a later point, you'd like to allow your users to show badges/medals/achievements/something on their profile.

With routing, you can achieve it like this:

$route['player/(:any)/(:any)'] = "player/show_$2/$1";
$route['player/(:any)'] = "player/show_profile/$1";

And your controller could in turn look like this:

class Player extends CI_Controller
{
  public function show_profile( $username )
  {
    // the profile info
  }

  public function show_badges( $username )
  {
    // the profiles badges
  }

  public function show_scores( $username )
  {
    // the profiles scores
  }
}

}

Basically, this allows you to simply add another method in your controller prefixing the method with show_ (like public method show_friends( $username ) )and you can access it instantly by going to /player/SomeDude/friends

Looking at the alternative, remapping your controller would allow you not to use routes, but write a controller like this:

class Player extends CI_Controller
{

  public function _remap($username, $params = array())
  {
    if(empty($username))
      show_404();

    $this->user = $this->user_model->find($username);

    if(count($params) == 0)
      $method = 'index';
    else
      $method = $params[0];

    unset($params[0]); //No need to send the method along as a parameter

    $method = 'process_'.$method;
    if (method_exists($this, $method))
    {
      return call_user_func_array(array($this, $method), $params);
    }
    show_404();
  }

  public method process_index()
  {
    // the profile info
  }

  public method process_badges()
  {
    // the profiles badges
  }

  public method process_scores()
  {
    // the profiles scores
  }

}

Personally, I like routing. I think it's transparent and makes my controllers look cleaner.

Sunday, October 9, 2022
3

you should add the following rule in your config/routes.php

// Profile Route
$route['(:any)'] = 'home/$1';

this will reroute all your request to the home controller.

so domain/michelle will re be rerouted as domain/home/michelle but in this case all your requests will be rerouted to the home controller eg. domain/pages/about will also be rerouted to domain/home/pages/about wich false.

domain/michelle => domain/home/michelle // TRUE
domain/pages/about => domain/home/pages/about // FALSE

so you MUST add for each request a identic rule before the Profile Route, eg. $route['pages/(:any)'] = 'pages/$1';

put it all together :

// Page Route 
$route['pages/(:any)']  = 'pages/$1'

// Last Profile Route
$route['(:any)'] = 'home/$1';
Saturday, August 20, 2022
1

Since you're using IIS 7, you can use the new rewriting support that's now available.

Take a look at http://www.iis.net/learn/extensions/url-rewrite-module/using-the-url-rewrite-module

Saturday, November 19, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :