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 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号