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.

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

报告相同问题?

悬赏问题

  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝