CodeIgniter Forums
Change route for root - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Change route for root (/showthread.php?tid=71502)

Pages: 1 2


Change route for root - omid_student - 08-21-2018

Hi
My url is www.localhost.com/11h11
when i use relate url,codeigniter show not found page
I need when user type relate url,i get 11h11 and process it
Thanks


RE: Change route for root - InsiteFX - 08-21-2018

Maybe _remap() or uri_segment()

If you use _remap() you can do almost anything that is on the url.


RE: Change route for root - omid_student - 08-21-2018

(08-21-2018, 02:09 PM)InsiteFX Wrote: Maybe _remap() or uri_segment()

If you use _remap() you can do almost anything that is on the url.

I tried use _remap but when i use localhost\myproject\11h11,it show 404 page
Default controller is whc.php
So i have to enter myproject\whc\11h11
And it is working but i need remove whc from url


RE: Change route for root - jreklund - 08-22-2018

Rename your function whc() into index() or change your route.php configuration.


RE: Change route for root - Pertti - 08-22-2018

Tried to reply to this yesterday but the site went down.

You can not use 11h11 as class, method or function name, it has to start with a letter or underscore.

_remap will re-route everything as long as you point it to the right controller.

so, if you want the URL to be www.myproject.com/11h11 you'll have to do it via routes.

application/config/routes.php
PHP Code:
$route['11h11'] = 'whc/my11h11'

PHP Code:
class Whc extends CI_Controller
{
    
// ...
    
public function my11h11()
    {
        echo 
'Im over here now!';
    }


If you want to make that tag dynamic, you probably need to rework your URLs to add www.myproject.com/tags/11h11 or something, so you can specify it should be treated as related URL.


RE: Change route for root - omid_student - 08-22-2018

(08-22-2018, 01:25 AM)Pertti Wrote: Tried to reply to this yesterday but the site went down.

You can not use 11h11 as class, method or function name, it has to start with a letter or underscore.

_remap will re-route everything as long as you point it to the right controller.

so, if you want the URL to be www.myproject.com/11h11 you'll have to do it via routes.

application/config/routes.php
PHP Code:
$route['11h11'] = 'whc/my11h11'

PHP Code:
class Whc extends CI_Controller
{
 
   // ...
 
   public function my11h11()
 
   {
 
       echo 'Im over here now!';
 
   }


If you want to make that tag dynamic, you probably need to rework your URLs to add www.myproject.com/tags/11h11 or something, so you can specify it should be treated as related URL.

Thank you
Ommm but 11h11 is dynamic and maybe 1h1 or 232h12
I try use remap but not working
I also try change route but is it not working
I think htaccess is good idea for redirect
Do you can help me about htaccess?


RE: Change route for root - Pertti - 08-22-2018

Updating your htaccess is well explained here - https://www.codeigniter.com/user_guide/general/urls.html?highlight=htaccess#removing-the-index-php-file

I can't remember in what order routing happens, but if the tag itself is dynamic, you have two options.

If you can allow controller reference in the URL - myproject.com/mycontroller/11h11 :
PHP Code:
class Mycontroller extends CI_Controller
{
    
// ...
    
public function _remap()
    {
        echo 
$this->uri->segment(2);
    }


If it must be first element of URL myproject.com/11h11 (this may not work, you need to test it yourself, but it may or may not override autolinking to controllers):
PHP Code:
$route['(:any)'] = 'whc/mymethod/$1'

PHP Code:
class Whc extends CI_Controller
{
    
// ...
    
public function mymethod($tag null)
    {
        echo 
$tag;
    }




RE: Change route for root - omid_student - 08-22-2018

(08-22-2018, 02:27 AM)Pertti Wrote: Updating your htaccess is well explained here - https://www.codeigniter.com/user_guide/general/urls.html?highlight=htaccess#removing-the-index-php-file

I can't remember in what order routing happens, but if the tag itself is dynamic, you have two options.

If you can allow controller reference in the URL - myproject.com/mycontroller/11h11 :
PHP Code:
class Mycontroller extends CI_Controller
{
 
   // ...
 
   public function _remap()
 
   {
 
       echo $this->uri->segment(2);
 
   }


If it must be first element of URL myproject.com/11h11 (this may not work, you need to test it yourself, but it may or may not override autolinking to controllers):
PHP Code:
$route['(:any)'] = 'whc/mymethod/$1'

PHP Code:
class Whc extends CI_Controller
{
 
   // ...
 
   public function mymethod($tag null)
 
   {
 
       echo $tag;
 
   }


Thanks
Is is OK


RE: Change route for root - InsiteFX - 08-22-2018

We had to get a user referral on access.

Below is how we did it, we create separate folders for each user under users.

This way you just access the site and it will go to index method.

PHP Code:
// ./application/controllers/Welcome.php

// --------------------------------------------------------------------

/**
 * index()
 *
 * index - default method called.
 *
 * @access    public
 */
public function index($urlCode NULL)
{

 
   if ( ! empty($urlCode))
 
   {
 
       // we have the code
 
   }
 
   else
    
{

 
   }

}


// --------------------------------------------------------------------

/**
 * _remap()
 *
 * Remaps the URL segments.
 *
 * Place at the bottom of controller.
 *
 * @access    private
 * @param    string
 * @param    array
 */
private function _remap($method$params = array())
{
 
   if (method_exists($this$method))
 
   {
 
       return call_user_func_array(array($this$method), $params);
 
   }

 
   show_404();
}

// ./apllication/config/routes.php
$route['welcome/(:any)']    = 'welcome/$1'

You need the if to decide on what you want to do with the input.


RE: Change route for root - omid_student - 08-22-2018

(08-22-2018, 03:33 AM)InsiteFX Wrote: We had to get a user referral on access.

Below is how we did it, we create separate folders for each user under users.

This way you just access the site and it will go to index method.

PHP Code:
// ./application/controllers/Welcome.php

// --------------------------------------------------------------------

/**
 * index()
 *
 * index - default method called.
 *
 * @access public
 */
public function index($urlCode NULL)
{

 
   if ( ! empty($urlCode))
 
   {
 
       // we have the code
 
   }
 
   else
    
{

 
   }

}


// --------------------------------------------------------------------

/**
 * _remap()
 *
 * Remaps the URL segments.
 *
 * Place at the bottom of controller.
 *
 * @access private
 * @param string
 * @param array
 */
private function _remap($method$params = array())
{
 
   if (method_exists($this$method))
 
   {
 
       return call_user_func_array(array($this$method), $params);
 
   }

 
   show_404();
}

// ./apllication/config/routes.php
$route['welcome/(:any)'] = 'welcome/$1'

You need the if to decide on what you want to do with the input.

it dont work
When i call localhost/myproject/11h11 with default controller welcome
It sho 404 page