Welcome Guest, Not a member yet? Register   Sign In
Use __remap to Show 404 Page
#1

[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


#2

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

[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.
#4

[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.
#5

[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])
#6

[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.
}
#7

[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...
#8

[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.
}
#9

[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.




Theme © iAndrew 2016 - Forum software by © MyBB