简体   繁体   English

如何从URL获取参数到cakePHP中的模型?

[英]How to get parameters from the URL to the model in cakePHP?

let's say i have: 假设我有:

http://some-domain/application/controller/action/parameter

This is somehow working in cakePHP. 这以某种方式在cakePHP中工作。 Now I want to now what exactly 'parameter' is. 现在,我想知道确切的“参数”是什么。 But inside the Model. 但是在模型内部。 How to get to this information? 如何获得这些信息?

I have to say that there is a formular including a 'Next' Button and I want to validate the input inside of the Model in beforeValidate() . 我必须说有一个包含“下一步”按钮的公式编写器,我想在beforeValidate()中验证模型内部的输入。 But I have to know on which page the user was at the time of clicking the submit button. 但是我必须知道单击提交按钮时用户在哪个页面上。 This page is 'parameter'. 此页面为“参数”。

Router::getParams() is available from everywhere and gives Router::getParams()随处可用,并提供

    [plugin] => 
    [controller] => leads
    [action] => step1
    [named] => Array()
    [pass] => Array()
    [url] => Array(
            [ext] => html
            [url] => someurl/post-1
    )

http://api.cakephp.org/2.3/class-Router.html#_getParams http://api.cakephp.org/2.3/class-Router.html#_getParams

There are two type of parameter in CakePHP, you have passed parameters and named parameters. CakePHP中有两种类型的参数,您已传递参数和命名参数。 A passed parameter is as shown in your example and will be passed as part of the url. 传递的参数如您的示例所示,并将作为url的一部分传递。

http://example.com/controller/action/passed_param
echo $this->params['passed'][0] // 'passed_param'

http://example.com/controller/action/name:param
echo $this->params['named']['name'] // 'param'

I would recommend getting the parameters in your controller and calling model methods with them passed through. 我建议您在控制器中获取参数,并通过它们调用模型方法。

Such as

$this->Model->find('all', array('conditions'=>array('id'=>$this->params['passed'][0])));

As to how it's working, you will want to have a look at your routes file. 关于它的工作方式,您将需要查看您的路线文件。 In your app/config/routes.php you will find all the routing and which parts are passed. 在您的app/config/routes.php您将找到所有路由以及传递了哪些部分。

The standard cake url format is usually as follows, as you'll see in the routes. 标准的蛋糕网址格式通常如下,如您在路线中所见。 array('controller'=>'MyController', 'action'=>'MyAction', 'MyParam');

I can't seem to find a specific page in the book on Params, but have a google around for guides. 我似乎无法在有关Params的书中找到特定的页面,但是在Google周围有指南。

Model (in MVC design pattern) shouldn't have direct access to any external variables. 模型(以MVC设计模式)不应直接访问任何外部变量。 The proper way is to pass that variable as a parameter from Controller or View: 正确的方法是将该变量作为参数从Controller或View传递:

$myModelObj->doSth($getParameter);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM