CodeIgniter Forums
Use __remap to Show 404 Page - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Use __remap to Show 404 Page (/thread-53621.html)



Use __remap to Show 404 Page - El Forum - 07-31-2012

[eluser]fjamal[/eluser]
I have the following code structure:

First Step:

Code:
class MY_Controller extends CI_Controller
it has the following __remap:

Code:
public function _remap($method, $param) {

  if (method_exists($this, $method)){

             call_user_func_array(array($this, $method), $param);

  } else {
                  
                      //show 404
  
  }
}

Second Step:


Code:
class Frontend_Controller extends MY_Controller
it has all implementations that are commonly shared in frontend.

Third Step:

Code:
class Home extends Frontend_Controller
Just a regular controller that handles user interactions.


Problem:

Whenever user types a wrong controller's name e.g.
Code:
http://www.example.com/home2

or
Code:
http://www.example.com/home/indexABC

Not found page is shown. However, repeating controller's name doesn't bring up the 404 page e.g.

Code:
http://www.example.com/home/home/home/home

The above url works totally fine as if user typed
Code:
http://www.example.com/home/

Any idea how to solve this issue? I need to show 404 page when user types double+ controller's name
Code:
http://www.example.com/home/home/


Thanks





Use __remap to Show 404 Page - El Forum - 07-31-2012

[eluser]Lewis Cowles[/eluser]
change
Code:
if (method_exists($this, $method)){
to
Code:
if ( (method_exists($this, $method)) && ($method != strtolower(get_class($this)) ) ){



Use __remap to Show 404 Page - El Forum - 07-31-2012

[eluser]Aken[/eluser]
Your _remap() method is basically emulating the exact method that CI itself uses to either call a valid controller method, or show a 404 page. I think you'd be better served by using routes rather than _remap().

The problem is that the method is found, and then all the remaining URI segments are passed as parameters to the method. You'd need to validate those parameters to establish whether or not they are valid, and then throw a 404 error if appropriate.


Use __remap to Show 404 Page - El Forum - 08-01-2012

[eluser]fjamal[/eluser]
[quote author="Lewis Cowles" date="1343784921"]change
Code:
if (method_exists($this, $method)){
to
Code:
if ( (method_exists($this, $method)) && ($method != strtolower(get_class($this)) ) ){
[/quote]

Previously, i tried this solution but did not help because _remap tends to catch methods of the given controller; index() is the default.

($method != strtolower(get_class($this))

The above code compares 'index' != 'home' even if i put multiple homes as /home/home/home.


Use __remap to Show 404 Page - El Forum - 08-01-2012

[eluser]fjamal[/eluser]
[quote author="Aken" date="1343784972"]Your _remap() method is basically emulating the exact method that CI itself uses to either call a valid controller method, or show a 404 page. I think you'd be better served by using routes rather than _remap().

The problem is that the method is found, and then all the remaining URI segments are passed as parameters to the method. You'd need to validate those parameters to establish whether or not they are valid, and then throw a 404 error if appropriate.[/quote]

I'm new to codeigniter, i don't know much about the framework structure and best practices of routings. For the time being i'm trying to build a basic structure that is fairly good enough to allow me complete tasks in a stable manner.

Here is my attempt:

Code:
http://localhost:8080/ci_centric/home/index/home

After dumping the parameter, home (the one after index) is stored in params. However, the params array is empty when i type this URL:

Code:
http://localhost:8080/ci_centric/home/home

The second home is supposed to be passed as parameter, but that's not the case. _remap deals with the second home as a controller. If i print $method, i get the value 'index'.

Note: i have htaccess that eliminates index.

Any idea to solve this issue?

All i need is preventing user from entering a duplicate controller's name beside the first controller as home/home (firstController/[strike]secondController[strike])



Use __remap to Show 404 Page - El Forum - 08-01-2012

[eluser]CroNiX[/eluser]
Code:
if ($this->router->fetch_class() === $this->router->fetch_method())
{
  //Houston, we have a problem.
}
Code:
if ($this->uri->segment(1) === $this->uri->segment(2))
{
  //Houston, we have a problem.
}



Use __remap to Show 404 Page - El Forum - 08-01-2012

[eluser]fjamal[/eluser]
[quote author="CroNiX" date="1343862446"]
Code:
if ($this->uri->segment(1) === $this->uri->segment(2))
{
  //Houston, we have a problem.
}
[/quote]

Perfect! Simple and elegant...


Use __remap to Show 404 Page - El Forum - 08-01-2012

[eluser]CroNiX[/eluser]
It probably won't work on a homepage though, as segment 1 will return FALSE as well as segment 2.

Maybe
Code:
if ($this->uri->segment(1) !== FALSE && $this->uri->segment(1) === $this->uri->segment(2))
{
  //Houston, we have a problem.
}



Use __remap to Show 404 Page - El Forum - 08-01-2012

[eluser]fjamal[/eluser]
[quote author="CroNiX" date="1343865319"]It probably won't work on a homepage though, as segment 1 will return FALSE as well as segment 2.

Maybe
Code:
if ($this->uri->segment(1) !== FALSE && $this->uri->segment(1) === $this->uri->segment(2))
{
  //Houston, we have a problem.
}
[/quote]

Yes, that's right, it didn't work on the homepage i.e.
Quote:http://www.example.com/ci_centric/
. However, this one
Quote:if ($this->uri->segment(1) !== FALSE && $this->uri->segment(1) === $this->uri->segment(2))
has solved the problem.

Have a good day.