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 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图