dtd793353 2014-04-21 23:45
浏览 69

如何在没有任何类型的框架的PHP项目中传递视图和控制器

Backgroud / Idea I am creating a web aplication from scratch; with out any type of php framework. I am creating the web application in this way; because i want to LEARN php; and i think that use a php framework do not will mame me a good php developer.

What i did so far? I create a project structure:

  • Project
    • css
    • script
    • Models
    • Controllers
    • Views
    • Resource

I created one view like:

<html>

<head>

<!-- here i call my jquery.js -->

<script>

...

function action()
(
...
    $.ajax(url: "/Controllers/HomeController.php", type:"POST",DataType:"JSON",Parameter:{var1:$("#var1").val(),method:"singup"})
    {...}
...
)

...

</script>

</head>

<body>

<input id="var1" name="var1" type="text">
<input id"mybutton" name="mybutton" type="button" onclick="action();">

</body>

</html>

In my HomeController.php

<?php

if(if(is_set($POST('method'))))
{
   if(function_exist($POST('method')))
   {   
      switch($POST('method'))
      {
          case "singup":
                $var1 = $POST('var1')
                singup($var1);
                break;
            ...
          default: break;
      }
   }
}

function singup($var1)
{
  $array = {"var1" => $var1};
  json_decode($array,1);
}

?>

Questions

I am not really sure about this code (security). I want to know: What is the recommended way make call between views and controllers with out use any type of php framework ?

  • 写回答

2条回答 默认 最新

  • dongxiaofa6359 2014-04-22 00:03
    关注

    Well, I'm not sure if i read your question correctly but...

    in your controller you do all the PHP stuff then when you want to load your view you can use the

    include command to load your html.php page. This way your PHP variables are exposed to your view.

    But if your learning, it doesn't hurt to crack open an existing framework and look around to see what approach they took.

    Also, in my framework I create a folder called "assets" and i include all the css, img, and js directories in it.

    As for any framework, I'll share with your the snippet i use to get the URI and pull out the controller,action, and params

    self::$_params = explode('/', rawurldecode(trim(parse_url(getenv('REQUEST_URI'), PHP_URL_PATH), '/')));
    
                $scope = array_shift(self::$_params);
    
    $pre = null;
                $pre = strlen(self::$_controller) > 1 && is_numeric(self::$_controller[0]) ? 'n' : null;
                $pre = strlen(self::$_controller) > 1 && self::$_controller[0] == '-' ? 'd' : $pre;
    
                self::$_controller = $pre . preg_replace('/-/', '_', self::$_controller) . '_Controller';
                self::$_callAction = array_shift(self::$_params);
    
                if (empty(self::$_callAction) ||  self::$_callAction == '-')
                {
                    self::$_callAction = 'indexAction';
    
                } else {
    
                    $pre = strlen(self::$_callAction) > 1 && is_numeric(self::$_callAction[0]) ? 'n' : $pre;
                    $pre = strlen(self::$_callAction) > 1 && self::$_callAction[0] == '_' ? 'u' : $pre;
                    $pre = strlen(self::$_callAction) > 1 && self::$_callAction[0] == '-' ? 'd' : $pre;
    
                    self::$_callAction = preg_replace('/-/', '_', self::$_callAction);
                    self::$_callAction = $pre . self::$_callAction . 'Action';
                }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算