【问题标题】:Passing multiple parameters via a url in MVC framework在 MVC 框架中通过 url 传递多个参数
【发布时间】:2013-04-15 15:54:31
【问题描述】:

我正在尝试在一个看起来像这样的 url 中传递多个参数...

http://somedomain.com/lessons/lessondetails/5/3

... 到控制器中看起来像这样的函数...

class LessonsController extends Controller

{
public function lessonDetails($studentId, $editRow=NULL)
{
    try {
        $studentData = new StudentsModel();
        $student = $studentData->getStudentById((int)$studentId);
        $lessons = $studentData->getLessonsByStudentId((int)$studentId);

        if ($lessons)
        {
            $this->_view->set('lessons', $lessons);

        } 
        else
        {
            $this->_view->set('noLessons', 'There are no lessons currently scheduled.');
        }
        $this->_view->set('title', $student['first_name']);
        $this->_view->set('student', $student);

        return $this->_view->output();

    } catch (Exception $e) {
        echo "Application error: " . $e->getMessage();
    }
}
}

但似乎只有第一个参数传递成功。 不知道要在这里复制和粘贴什么,但这里是 bootstrap.php...

$controller = "students";
$action = "index";
$query = null;

if (isset($_GET['load']))
{
    $params = array();
    $params = explode("/", $_GET['load']);

    $controller = ucwords($params[0]);

    if (isset($params[1]) && !empty($params[1]))
    {
            $action = $params[1];
    }

    if (isset($params[2]) && !empty($params[2]))
    {
            $query = $params[2];
    }
}

$modelName = $controller;
$controller .= 'Controller';
$load = new $controller($modelName, $action);

if (method_exists($load, $action))
{
    $load->{$action}($query);
}
else
{
    die('Invalid method. Please check the URL.');
}

提前致谢!

【问题讨论】:

  • ...您的问题是什么?什么没有按预期工作?你用的是什么 MVC 框架?
  • 向我们展示您的路由代码。
  • 基于htaccess,看起来您正在使用 Zend Framework。这是您的routing 的问题,而不是您的.htaccess
  • 糟糕,抱歉。使用上面的代码,只有第一个参数成功传递给函数。
  • 没错,为了能够帮助您,我们需要查看您定义的路线。

标签: php url-routing


【解决方案1】:

从您的动作控制器调用$this->getRequest()->getParams() 并检查两个参数是否都存在。

如果否,则问题出在您的路由上。

如果是,问题在于将参​​数传递给您的控制器方法lessonDetails

另外,lessondetailsAction 也不见了。 (如果您访问您发布的网址,这将是调用的方法)

【讨论】:

猜你喜欢
  • 2018-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-22
  • 1970-01-01
相关资源
最近更新 更多