在 MVC 中分解 url 和传递部分

发布于 2024-12-23 09:38:05 字数 1356 浏览 1 评论 0原文

我正在尝试创建一个 MVC 应用程序,目前正在处理 Bootstrap 文件。我获取 URL 并将其展开,然后将各个部分分配给控制器方法和方法参数。但是,我找不到将多个参数传递给该方法的方法。

mysite/newuser/login/user_name/user_pass

newuser -> Controler of the site
login   -> currently used method

user_name -> first argument
user_pass -> second_argument

例如

$url = "mysite/newuser/login/user_name/user_pass";

$path = expload('/',$url);


$this->controler = $path[0];
$this->method    = $path[1];

对于参数,我创建了第二个数组,如下所示:

// Set the substring path as method properties
if (isset($path[2])) {

    $this->url_sub_path = $path[2];

    $sub_path = explode('/', $this->url_sub_path);
     if (isset($sub_path)) {

         $this->model_properties = $sub_path;

当我为控制器分配一组时

$site_controler = $this->controler;
include CONTROLER.$site_controler . '.php';

$new_instans = new $site_controler();

但问题出在这里:

$site_method = $this->model;
$new_instans->{$site_method}($this->model_properties);

$this->model_properties 是数组

如果函数是:

public function login($user_name,$user_pass){
    // some code
}

我需要传递 url 属性,它们是数组,并且我的函数中有两个变量 这个想法是将数组转换为变量

或者你可以传递一个关于如何将参数从 URL 传递到我的模型的想法

I am trying to create an MVC application and am currently working on the Bootstrap file. I get the URL and explode it then assign the parts to the Controller Method and Method arguments. But, I can't find a way to pass multiples arguments to the method.

mysite/newuser/login/user_name/user_pass

newuser -> Controler of the site
login   -> currently used method

user_name -> first argument
user_pass -> second_argument

For example

$url = "mysite/newuser/login/user_name/user_pass";

$path = expload('/',$url);


$this->controler = $path[0];
$this->method    = $path[1];

For the arguments I create a second array like this:

// Set the substring path as method properties
if (isset($path[2])) {

    $this->url_sub_path = $path[2];

    $sub_path = explode('/', $this->url_sub_path);
     if (isset($sub_path)) {

         $this->model_properties = $sub_path;

When i assign the controllers a set

$site_controler = $this->controler;
include CONTROLER.$site_controler . '.php';

$new_instans = new $site_controler();

But the problem is here:

$site_method = $this->model;
$new_instans->{$site_method}($this->model_properties);

$this->model_properties is Array

if the function is:

public function login($user_name,$user_pass){
    // some code
}

I need to pass the url properties they are Array and I have two variables in my function
The idea is to convert the array to variables

Or you could pass an idea in how to pass arguments from URL to my model

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆凄美了谁 2024-12-30 09:38:05

试试这个函数: http://www.php.net /manual/en/function.call-user-func-array.php

call_user_func_array ($new_instans->{$site_method} , $this->model_properties )

Try this function: http://www.php.net/manual/en/function.call-user-func-array.php

call_user_func_array ($new_instans->{$site_method} , $this->model_properties )
酒绊 2024-12-30 09:38:05

如果,正如您所说, $this->model_properties 是一个数组,您可以执行以下两种操作之一。

情况1:维护函数声明,并在调用函数之前访问数组的元素。

login() 函数(维护其声明):

public function login($user_name,$user_pass){
 // some code
}

要调用该函数,请执行以下操作:

$array = $this->model_properties;
$param1 = $array[0]; //The numeric index may vary, depending on how this array was populated
$param2 = $array[1];
$new_instans->{$site_method}($param1, $param2);

情况 2: 更改函数声明以接收数组,并访问数组的元素函数内部。

login() 函数,更改声明:

public function login($arrayParams){
    //Access the parameters like this
    $param1 = $arrayParams[0]; //The numeric index may vary, depending on how this array was populated
    $param2 = $arrayParams[1];

    //The rest of your code...
}

要调用该函数,只需传递数组,就像您已经在做的那样:

$new_instans->{$site_method}($this->model_properties);

无论您选择哪个版本来解决问题,重要的部分是:

$param1 = $array[0];
$param2 = $array[1];

在这里,您将索引 01 的数组元素的内容分配给一个变量,从而允许您独立处理这些值。

If, as you say, $this->model_properties is an array, you can do one of two things.

Case 1: Maintain the function declaration, and access the array's elements before calling the function.

The login() function (maintain it's declaration):

public function login($user_name,$user_pass){
 // some code
}

To call the function, do this:

$array = $this->model_properties;
$param1 = $array[0]; //The numeric index may vary, depending on how this array was populated
$param2 = $array[1];
$new_instans->{$site_method}($param1, $param2);

Case 2: Change the function declaration to receive an array, and access the array's elements inside the function.

The login() function, change the declaration:

public function login($arrayParams){
    //Access the parameters like this
    $param1 = $arrayParams[0]; //The numeric index may vary, depending on how this array was populated
    $param2 = $arrayParams[1];

    //The rest of your code...
}

To call the function simply pass the array, as you're already doing:

$new_instans->{$site_method}($this->model_properties);

Independently of which version you choose to solve your problem, the important part is this:

$param1 = $array[0];
$param2 = $array[1];

Here, you assign the content of the array's elements of index 0 and 1 to a variable, allowing you to treat those values independently.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文