doulu1544 2014-04-25 05:00
浏览 25
已采纳

转换为原始PHP代码,用于CakePHP,新蛋糕

I'm a total newbie to CakePHP, but I have a fairly ok grasp on the fundamentals of raw PHP. My problem is getting over the hump and learning how to conform to the CakePHP framework. I've read up on Cake and have done the examples, but the only way I'm going to learn this is to jump in head first and start coding something on my own. My question is, how do I convert something this simple into cake. IE what goes where, Controller/Model/View.

<?php
if (isset($_POST['guess'])) {
//my php 
};
?>

<form name="form" method="post" action="#">
Your guess: <input type="text" name="guess">
<input type="submit" value="Submit">
</form>

All I want to do here is pass a number from user input to php (no database connection), run some php calculations, then return the results to the view. Do I have to use the form helper? It seems to center around database queries... And what goes where? From my angle, it seems cake may complicate such a simple snippet.

Much appreciated if someone could make some connections/relations to what I'm use to, and get me rolling.

  • 写回答

1条回答 默认 最新

  • doudou3716 2014-04-25 05:23
    关注

    I recommend to read CakePHP´s Blog Tutorial.

    At first, create a view located in app/View/Guess/create.ctp:

    // Using Form-Helper
    <?php echo $this->Form->create(); ?>
    <?php echo $this->Form->input('guess'); ?>
    <?php echo $this->Form->end('Submit'); ?>
    

    At second, create a controller located in app/Controller/GuessController.php:

    class GuessController extends AppController
    {
        // stop using a model
        public $uses = array();
    
        function create()
        {
            if($this->request->is('post', 'put'))
            {
                // now do something with your form data
                $doSomething = $this->request->data['Guess']['guess'];
            }
        }
    }
    

    That´s it. Now you should call your action via url http://examplehost.com/Guess/create

    At the beginning, CakePHP seems to be complicated, but it isn´t. I think, it is important to understand the MVC. That makes it easier to learn all common PHP-Frameworks.

    At a first glance, a simple form like above might be implemented easier via pure php and html code. But if you want to do deeper tasks (e.g. validation, authentication, autorisation, security ...) and want to have a clean code, you will recognize the advantages of that framework.

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

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)