douhuxi4145 2017-06-22 23:09
浏览 36
已采纳

PHP MVC:我如何通过按钮路由控制器?

I am new to Php mvc design and I have been reading a tutorial, Simple MVC for beginners. In this tutorial the author is routing all calls from the index.php file based on some link. I have a landing page written in html and was wondering how would i route my, lets say when a user clicks the sign up button, sign up view based on a button not link?

That brings me to another question. Would a controller even be necessary going from a landing page view to a signup view?

views/layout page:

<DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <header>
      <a href='/php_mvc_blog'>Home</a>
    </header>

    <?php require_once('routes.php'); ?>

    <footer>
      Copyright
    </footer>
  <body>
<html>

index.php:

<?php


require_once('connection.php');

  if (isset($_GET['controller']) && isset($_GET['action'])) {
    $controller = $_GET['controller'];
    $action     = $_GET['action'];
  } else {
    $controller = 'pages';
    $action     = 'home';
  }

  require_once('views/layout.php');
?>
  • 写回答

1条回答 默认 最新

  • douguanya6399 2017-06-23 00:03
    关注

    This is kind of hard to answer as it's not really related to MVC. Hopefully I can give you some insight to point you in the right direction.

    MVC is just a concept on how you organise your code. There are many different ways to setup a site which could still be considered MVC.
    To put it simply:

    • Model: Deals with database logic, one model can be shared by many controllers.
    • View: Where your html is (which could include php variables if you aren't using js).
    • Controller: Where the logic for your php is. e.g. Mapping an array for your view, going to a different page etc.

    Question 1: Your question isn't exactly dealing with the MCV concept itself and more just how html interacts with php.

    1. You have a link or a button with a link in it.
    2. This link points to your route which then calls your controller.
    3. Your controller runs the php logic you need and then serves the html to the user.

    Question 2: If you do not have any php in your html view (like an faq page which doesn't require logic) you can just link directly to the page. However it's probably best to go through a controller which just renders the page to keep it consistent.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效