Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using multiple parameters #26

Open
jmjuju opened this issue Sep 7, 2017 · 6 comments
Open

Using multiple parameters #26

jmjuju opened this issue Sep 7, 2017 · 6 comments

Comments

@jmjuju
Copy link

jmjuju commented Sep 7, 2017

If someone has multiple parameters:

eg: http://example.com/controller/method/param1/param2/param3/

Then call_user_func_array only allows one parameter to be passed to the method ("param1") as a string.
Replacing 'call_user_func_array' with 'call_user_func' on line 44 of Application.php will enable the user to have multiple parameters sent as an array.

Given the line in the splitUrl function:

$this->url_params = array_values($url);

One can only assume that passing parameters as an array was always the intention? Or am I mistaken (My php is intermediate at best...so I might be missing something here..)

@lotfio
Copy link

lotfio commented Sep 7, 2017

No you are wrong sir it is working perfectly just pass the url parameters to your controller method and then you can use them . you said that replacing call_user_func_array with call_user_func will solve the problem ?? have you tried that ? how would that be possible when we have to call an object method by parameters so we need the object the method and parameters like this call_user_func_array(array($this->url_controller, $this->url_action), $this->url_params); while call_user_func can only call a function with parameters (outside an object)

@jmjuju
Copy link
Author

jmjuju commented Sep 8, 2017

For me, when I changed it to call_user_func , it worked.
In my specific situation, my controller is "Dashboard" and my method "org". If I call example.com/dashboard/org/param1/param2/ , and If I uncomment the debug lines in the splitUrl function, it returns:
Controller: dashboard Action: org Parameters: Array ( [0] => param1 [1] => param2 )

Therefore, url_params is confirmed as an array as above. It's then passed to the controller and method/action as per line 44 in Application.
However, if my method is simply:

public function org($params){ echo gettype($params); }

it returns string.
If I change line 44 to call_user_func but change nothing else (Ie: the arguments stay exactly the same), the above function returns an array.

HOWEVER, If I change line 44 to:

call_user_func_array(array($this->url_controller, $this->url_action), array($this->url_params));

This also works. It's like I have to declare url_params as an array type again for it to work. Somehow call_user_func_array almost converts url_params into a string because it's not explicitly stated as an array in the argument line (even though it's set as an array as per private $url_params = array();

So - PHP bug perhaps? Again, my skill is intermediate at best, so there might be something expected/normal that im missing here.

@hozzaq
Copy link

hozzaq commented Sep 21, 2017

this is what i do for multiple parameter

call_user_func_array(array($this->url_controller, $this->url_action), array($this->url_params));
// url_action no longer get parameter for easier multiple parameter
// now you can do like //site/controller/action/param1/value1/param2/value2 and so on
if (count($url) > 2 && count($url) % 2 == 0) {
    unset($url[0], $url[1]);
    $param = array_values($url);
    for ($i=0; $i < count($param); $i++) {
        if ($i % 2 == 0) {
            $params[$param[$i]] = $param[$i+1];
        }
    }
    $this->url_params = $params;
}

@jgodstime
Copy link

@hozzaq, Please where do i add this code?

@hozzaq
Copy link

hozzaq commented Nov 20, 2017

@jgodstime hozzaq@9fb3578

@mhco
Copy link

mhco commented Mar 29, 2018

So this is actually already done in the base code, it's just a bit wonky and undocumented.

When you go to http://example.com/controller/method/param1/param2/param3 you should hit the /controller/method/ page, with param1-3 passes as arguments (provided controller and method exist, but I'll get into that later). In order to call the parameters, you either have to declare them in the method, like:

public function index($param1 = null, $param2 = null, $param3 = null)

OR! Or you can leave the declarations out, and use func_get_args like so:

public function index()
{
    $params = func_get_args();
    print_r($params);
}

Now, if you have a page, like /songs/ that you want to be able to do http://example.com/songs/1 or http://example.com/songs/1/edit etc, you can place the following code just after line 54:

elseif (is_numeric($this->url_action)) {
    $this->url_params = array_merge([$this->url_action], $this->url_params);
    $this->url_controller->index($this->url_params);
}

The only problem I've run into with this method is that your controller file can get a bit messy if you have a bunch of methods in that file.

This isn't really an issue with the way the site works (because you'd want /songs/bldfkjashfk to return a 404 error page), but more of what happens when a project doesn't have detailed documentation like the big projects tend to have. This can easily be remedied by writing a bit of a "how-to" on this, so it's not as confusing. Also, since this is just a framework, it's not meant to be the 100% solution to every problem you'll face, just a strong base to start your work from; some assembly will be required.

@panique You can probably close this issue, and open an ongoing task about creating documentation for the project to alleviate any future confusion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants