Welcome Guest, Not a member yet? Register   Sign In
Using the remap with parameters
#1

(This post was last modified: 10-23-2018, 10:52 AM by Qodi.)

Do both of these forms work

$this->$method($params);

call_user_func_array(array($this, $method), $params)


Thanks in advance.

God bless.
Reply
#2

(This post was last modified: 10-20-2018, 03:30 PM by jreklund. Edit Reason: Added an example. )

Second are preferred as $params in _remap are an array. And if there are only one item in that array, it will convert it into a string.
With the first option you are going to get Array to String converting error.

Also it will allow you to pass more then one param into that function e.g. index($arg, $arg2). First option gives you the entire array in $arg, and ignores $arg2.

Here's an example I wrote to another forum member. If the first parameter matches a function it will call that, else it will pass it into index().
So http://localhost/create will go to create() and http://localhost/everything-else will go to index('everything-else').
PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Products extends CI_Controller {

    public function 
index($category='all')
    {
        echo 
'Category: ' $category;
    }

    public function 
create()
    {
        echo 
'The create() method is called...';
    }

    public function 
_remap($method$params = array())
    {
        if (
method_exists($this$method)) {
            return 
call_user_func_array(array($this$method), $params);
        } else {
            if( empty(
$params) ) { $params[] = $method; }
            return 
call_user_func_array(array($this'index'), $params);
        }
        
show_404();
    }

Reply
#3

(This post was last modified: 10-21-2018, 07:41 AM by Qodi.)

(10-20-2018, 03:21 PM)jreklund Wrote: Second are preferred as $params in _remap are an array. And if there are only one item in that array, it will convert it into a string.
With the first option you are going to get Array to String converting error.

Also it will allow you to pass more then one param into that function e.g. index($arg, $arg2). First option gives you the entire array in $arg, and ignores $arg2.

Here's an example I wrote to another forum member. If the first parameter matches a function it will call that, else it will pass it into index().
So http://localhost/create will go to create() and http://localhost/everything-else will go to index('everything-else').
PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Products extends CI_Controller {

    public function 
index($category='all')
    {
        echo 
'Category: ' $category;
    }

    public function 
create()
    {
        echo 
'The create() method is called...';
    }

    public function 
_remap($method$params = array())
    {
        if (
method_exists($this$method)) {
            return 
call_user_func_array(array($this$method), $params);
        } else {
            if( empty(
$params) ) { $params[] = $method; }
            return 
call_user_func_array(array($this'index'), $params);
        }
        
show_404();
    }


Thanks, I'll study this form.

Just to double check, even if I use the form $params = array() in the remap parameter list it will still try to convert the one entry array to a string?
Reply
#4

I haven't tried to force an associate/multi array in the first position of $params. As that's not something that can be generated from the URL. But a normal array of ['one','two','three'] will turn into index($one,$two,$three) and ['one'] will turn into index($one).

http://localhost/one/two/three/ turns into ['one','two','three'].
http://localhost/one/ turns into ['one'].
Reply




Theme © iAndrew 2016 - Forum software by © MyBB