Welcome Guest, Not a member yet? Register   Sign In
Modular Extensions 5.4 - Determine URI segment of method
#1

[eluser]Aeisor[/eluser]
Is there any easy way to determine the index value of the method that has been called using MX? Before MX I had used
Code:
$CI->uri->uri_to_assoc(n);
where n was either 3 or 4 depending on if
Code:
method_exists($this, $method)
was true or false; However, with MX I also need to factor in module directories.

I used this to create an array of key=>value pairs from the request, without using ?key=value&key2=value2

I had a quick look in the code, but nothing jumped out. I could just not be looking hard enough.
#2

[eluser]Aeisor[/eluser]
As a workaround, I've put this together which on initial testing seems to work:

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

  $CI=&get;_instance();

  $CI->uri->params=array();

  $class_method = $this->router->class.'/'.$this->router->method.'/';
  if(strpos($this->router->uri->uri_string, $class_method) !== FALSE) {
   $segments = explode('/',
    substr($this->router->uri->uri_string,
     strpos($this->router->uri->uri_string, $class_method)+strlen($class_method)
    )
   );

   $count = count($segments);
   for($i = 0; $i < $count; $i+=2) {
    if(isset($segments[$i]) && isset($segments[$i+1])) {
     $CI->uri->params[$segments[$i]] = $segments[$i+1];
    }
   }
  }

  if(!method_exists($this, $method)) {
   $this->index();
  }
  else {
   call_user_func([$this, $method]);
  }
}

$CI->uri->params ends up as an associative array of key => value pairs

Note: This only works if the URI has the method in it, otherwise CI gets confused and tries to call the first key as the method. I'll update if I find issues with it.

Also Note: If for some reason you have a directory structure like:
Code:
application/modules/welcome/controllers/welcome/welcome.php
which has a method of 'welcome', your URI would end up as something like
Code:
(base_url)/welcome/welcome/welcome/
which would break. In this instance you may want to use strrpos instead of strpos. Let's hope you don't also have a key => value pair of welcome => welcome.
#3

[eluser]TheFuzzy0ne[/eluser]
Try:
Code:
$method = $this->router->method;
#4

[eluser]Aeisor[/eluser]
You'll notice in my workaround that I used $this->router->method; However, I was looking for the URI Segment's index value

Code:
$this->uri->segment(n);

So I could use

Code:
$this->uri->uri_to_assoc(m);
#5

[eluser]TheFuzzy0ne[/eluser]
Sorry, I totally misunderstood what you are asking to do.

For now, here's a replacement for what you're using. It should do the same thing with a lot less effort.

Code:
public function _remap($method, $params)
{
    $_params = array();

    for ($i = 0; isset($params[$i]); $i += 2)
    {
        if (isset($params[$i + 1]))
        {
            $_params[$params[$i]] = $params[$i + 1];
        }
    }

    get_instance()->params =& $_params;

    if(method_exists($this, $method))
    {
        return call_user_func_array([$this, $method], $params);
    }
    
    $this->index();
}

I'm using MXHMVC, and $this->uri->uri_to_assoc() is working fine for me, hence why I'm a little confused about what you're trying to do. I'm pretty new to the whole HMVC-thing, but as far as I'm aware, the module name in the URI pretty much replaces the controller name, so the URL format is identical to what it would have been if you weren't using HMVC.

If you can explain in more detail what it is you are trying to do, and what it's meant to achieve, we might be able to offer you a much more straight-forward solution. Any examples of the URI and some example code illustrating what's not working in your current setup, and the result you are expecting would help a lot.

Sorry for being dense... It's the end of a long day.
#6

[eluser]Aeisor[/eluser]
Thanks for your reply.

Essentially I want to be able to pass parameters through in key => value pairs, for example: /welcome/index/language/en-GB. The welcome controller is located at ./application/modules/welcome/controllers/welcome.php

In this instance I can use $this->uri->uri_to_assoc(3); and I'll get language => en-GB; However, if I used $this->uri->uri_to_assoc(3) on the URL /welcome/test/index/language/en-GB, where the controller is now ./application/modules/welcome/test/controllers/test.php, the key => value pairing is messed up.

So what I wanted to do was find the index of the method (in both cases, 'index', but the numeric index in the URI Segments changed) so that I could use that in $this->uri->uri_to_assoc(n) to get the correct key => value pairings.

In the first example, I would want something to return 2 (So I can +1 = 3). In the second example, I'd want it to return 3 (So I can +1 = 4)

Hope that makes sense?
#7

[eluser]TheFuzzy0ne[/eluser]
Hi. Sorry for the delay. I was under obligation to watch a movie with the missus last night before going to bed.

I've had a think about this, and all of the ways I can think of (for now, at least), seem a little hackish, and aren't bulletproof. I'm going to continue to think about this whilst I'm out today. Hopefully I can come up with something more solid. I was thinking about extending the Router class. I'll keep you posted.
#8

[eluser]TheFuzzy0ne[/eluser]
This has been bakin' my noodle for a little over a day now, however, I think I have your solution. Try using:
Code:
$this->uri->ruri_to_assoc();

That seems to yield the correct result each time.

Hope this helps.
#9

[eluser]Aeisor[/eluser]
Good shout. It seems to work just fine and doesn't require messing about

Thanks for your help.




Theme © iAndrew 2016 - Forum software by © MyBB