CodeIgniter Forums
Routing help needed - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Routing help needed (/showthread.php?tid=60607)



Routing help needed - El Forum - 05-08-2014

[eluser]Naoshad[/eluser]
Hello,

Greetings. In one of my current application I need this: www.example.com/WaQw2rD my users will request url like that and I need to pass WaQw2rD to the default controller.

how can I do it?

Any help will be highly appreciated.

Thanks a lot.


Routing help needed - El Forum - 05-08-2014

[eluser]Tpojka[/eluser]
Controller could be like this:
Code:
<?php if ( ! defined ('BASEPATH')) exit('No direct script access allowed!');

class User_provided extends CI_Controller
{
  public function __construct()
  {
    parent::__construct();
  }

  public function index($myVar)
  {
    $myVar = $this->uri->segment(1);
    if ($myVar == FALSE OR $myVar == NULL)
      redirect('home', 'refresh'); // or where ever
    /*
      here write block of code you need to do with variable
    */
  }
}
In APPPATH . 'config/routes.php' add this:
Code:
$route['(:any)'] = "user_provided/index/$1";
Make sure you set wild card for routing after all defined routes.
Maybe helps.


Routing help needed - El Forum - 05-08-2014

[eluser]Naoshad[/eluser]
Thanks a lot Tpojka for your reply.

Could you please help me to understand this? "Make sure you set wild card for routing after all defined routes."

Thanks again. Smile


Routing help needed - El Forum - 05-08-2014

[eluser]Tpojka[/eluser]
In post above I wrote route rule for anything that is set after base_url().
So if you have route like:
http://www.example.com/contact
user_provided.php will be used instead other controller you coded for contact page and there fore,
you need to set specific routes before wild card route.
Code:
$route['contact'] = "pages/contact";
$route['about'] = "pages/about";
$route['(:any)'] = "user_provided/index/$1";

Read that on this page.


Routing help needed - El Forum - 05-08-2014

[eluser]Naoshad[/eluser]
Thanks a lot... it worked like a charm Smile

Thank you very much.


Routing help needed - El Forum - 05-08-2014

[eluser]Naoshad[/eluser]
Hi,
One more question. For example if I have another controller there names dahboard and there are methods in this controller. Now if I want to access

Code:
www.example.come/dashboard/method1

or

Code:
www.example.come/dashboard/method2

it doesn't hit that specific method though I added this:

Code:
$route['dashboard'] = "dashboard";
$route['(:any)'] = "welcome/index/$1";

how can I make all methods accessible?

Thanks.


Routing help needed - El Forum - 05-08-2014

[eluser]Tpojka[/eluser]
[quote author="Naoshad" date="1399573591"]Thanks a lot... it worked like a charm Smile

Thank you very much. [/quote]
I am glad if I was helpfull to you.

If you have route rule with wild card key (:any), you would need to set all other routes before that.
I mentioned that before. Any specific URL should be called has to be assigned to it's concrete value trough routes.php file.
Example:
In URLs
Code:
www.example.com/controller
www.example.com/controller/method
www.example.com/controller/method/argument
If in routes.php you have:
Code:
$route['(:any)'] = 'controller';
[/code]
any of those links will call first one to be executed because
wild card :any can mean anything of that after base_url() regardless segments or any other specific characters.