ThinkPHP basic notes

Source: Internet
Author: User
1. delete the cache in time after the cache template is modified. Otherwise, the modification will not take effect. 2. controller 2.1 assign values to variables note that & amp; #39; & amp; #39; is used for variables, rather than $ this-& gt; assign (& amp; #39; str & amp; #39;, & quot; H...

1. cache

You must delete the cache immediately after modifying the template. Otherwise, the modification will not take effect.

2. Controller

2.1 variable assignment

Note that ''is used for the variable, not $

$ This-> assign ('str', "Hello ThinkPHP! ");

Instead:

$ This-> assign ($ str, "Hello ThinkPHP! ");

2.2 Template path

$ This-> display ('default/Index/index'); do not add a suffix (.html or other); otherwise, the following error is reported.

The template does not exist [./Tpl/default/Index/index.html.html]

2.3 redirection

$ This-> redirect ("index ");

2.4 Model table

For example, $ Form = D ("Demo"); corresponds to the DB_PREFIX.demo table.

2.5 successes and errors

$ This-> success ("data written successfully"); if this is defined, you need to create a./Tpl/default/Public/success.html template.

$ This-> error ("data writing failed ");

Header ("Content-Type: text/html; charset = utf-8 ");

Exit ($ Form-> getError (). '[return]');

Main variables: {$ msgTitle} {$ message} {$ error} {$ waitSecond} {$ jumpUrl}

3. Template

3.1 Variable display

{$ Str}

3.2 form post

The form method must be specified as post. I have encountered an inexplicable error because this is not specified.

3.3 present label

.... Determine whether a variable is defined

4. common variables and constants

. Website root directory address

: Current project (entry file) address

_ URL __:

_ PUBLIC _: PUBLIC directory of the website, not the directory under the topic

_ CURRENT _: Template directory of the CURRENT module

APP_PATH: Current Project directory

LIB_PATH: Directory of the project class library

LANG_PATH: Project language file directory

COMMON_PATH: public file directory of the project

APP_PUBLIC_PATH: Project Public File Directory

APP_TMPL_PATH: Directory of the project template

TEMPLATE_PATH: current template path

WEB_PUBLIC_PATH: public directory of the website

More constants: http://www.thinkphp.cn/Manual/221

5. common functions

Highlight_file (LIB_PATH. 'action/IndexAction. class. php '); Highlight files. this is a function in PHP.

6. common code snippets

6.1 IndexAction. class. php

  1. Class IndexAction extends Action
  2. {
  3. Public function index ()
  4. {
  5. $ Form = D ("Form ");
  6. $ List = $ Form-> select ();
  7. $ This-> assign ('list', $ list );
  8. $ This-> display ('./Tpl/default/Index/index ');
  9. }
  10. Public function insert ()
  11. {
  12. $ Form = new Model ("Form ");
  13. If ($ Form-> Create ()){
  14. If (false! ==$ Form-> add ()){
  15. $ This-> success ("data written successfully ");
  16. } Else {
  17. $ This-> error ("data writing failed ");
  18. }
  19. } Else {
  20. Header ("Content-Type: text/html; charset = utf-8 ");
  21. Exit ($ Form-> getError (). '[return]');
  22. }
  23. }
  24. }
  25. ?>

6.2 FormModel. class. php

  1. Class FormModel extends Model {
  2. // Automatic verification settings
  3. Protected $ _ validate = array (
  4. Array ('title', 'require ', 'The title is required! ', 1 ),
  5. Array ('email ', 'Email', 'mailbox format error! ', 2 ),
  6. Array ('content', 'require ', 'content required '),
  7. Array ('title', '', 'title already exists ', 0, 'Unique', self: MODEL_INSERT ),
  8. );
  9. // Automatic filling settings
  10. Protected $ _ auto = array (
  11. Array ('status', '1', self: MODEL_INSERT ),
  12. Array ('create _ time', 'time', self: MODEL_INSERT, 'Function '),
  13. );
  14. }
  15. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.