在 MVC 中分解 url 和传递部分
我正在尝试创建一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个函数: http://www.php.net /manual/en/function.call-user-func-array.php
Try this function: http://www.php.net/manual/en/function.call-user-func-array.php
如果,正如您所说,
$this->model_properties
是一个数组,您可以执行以下两种操作之一。情况1:维护函数声明,并在调用函数之前访问数组的元素。
login()
函数(维护其声明):要调用该函数,请执行以下操作:
情况 2: 更改函数声明以接收数组,并访问数组的元素函数内部。
login()
函数,更改声明:要调用该函数,只需传递数组,就像您已经在做的那样:
无论您选择哪个版本来解决问题,重要的部分是:
在这里,您将索引
0
和1
的数组元素的内容分配给一个变量,从而允许您独立处理这些值。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):To call the function, do this:
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:To call the function simply pass the array, as you're already doing:
Independently of which version you choose to solve your problem, the important part is this:
Here, you assign the content of the array's elements of index
0
and1
to a variable, allowing you to treat those values independently.