Thinkphp Project Summary

Source: Internet
Author: User
Tags smarty template

1, when you require thinkphp, will automatically generate in the app directory Common, Home, Runtime three folders.

2. Input URL Http://localhost/blog/app/index.php/home/index/index appears

Unable to load controller:index error location FILE: D:\xampp\htdocs\blog\ThinkPHP\Library\Think\App.class. PHP line : 90

Cause resolution: It is case sensitive by default and we can change the settings for convenience:

<? PHP return Array (  //URL address case sensitive setting  ' url_case_insensitive '  = =  true,  );

3, create a new controller, note the namespace, and inheritance.

<? phpnamespace Home\controller;  Use Think\controller; class extends controller{  function  login () {    $this , display ();}  } 

4, when the controller is written, we need to add the corresponding view file.

5, connect the database. We also need to change the settings for the connection to the database. Here we are using MySQL, as shown in. If the port number used is the default and the password is empty, you do not need to fill it out. Note the name and prefix of the database.

6, before the operation of the database, we must first build a model class, aspects and subsequent use. We are in the same directory with home to create a new Model folder, write good code.

7, then we start to call Model to manipulate the database, enter the URL, see the following content to know that the connection database is successful.

8, Next, we have written before the front end and background HTML and other files are put in, in order to facilitate the subsequent operation, we want to classify the file, the final effect is as follows:

Which public store some styles, JS files, pictures and so on.

9, if we want to access these HTML files, we must also add the corresponding controller and so on. Enter the URL, visit, we will find the page missing pictures and styles. Due to changes in CSS, JS file location, we have to re-modify these URLs or the SRC attribute. Because these files are often used, we add constants and modify them in the HTML file. So we can see the final page.

10, using Smarty template. Just change the settings just fine.

11, the following we began to demonstrate the registration module, which involves a lot of content.

First we write a registered Model, which is primarily used to validate the data.

<?phpnamespace Model; UseThink\model;classUsermodelextendsmodel{protected $patchValidate=true;//Batch Verification  protected $_validate=Array(    Array(' username ', ' require ', ' username must! ‘),//validation by default with regular    Array(' Password ', ' Require ', ' password must! ‘),Array(' Password2 ', ' password ', ' Two must be equal ', 0, ' confirm '),Array(' User_email ', ' email ', ' Mail format error ', 0),  );}

Yu

At the same time we need to write the corresponding code on the controller. Too long, no, or put the code

<?phpnamespace Home\controller; UseThink\controller;classUsercontrollerextendsController {functionLogin () {$this-display (); }  functionRegister () {//Call Usermodel    $user=New\model\usermodel (); if(!Empty($_post)){      $z=$user-Create (); if(!$z){        Var_dump($user-GetError ()); }      //add data to the database      $rst=$user-Add (); //determine whether to add success      if($rst){        Echo"Success"; }Else{        Echo"Error"; }    }    $this-display (); }}

12, Login module. When the user logs in, the user name and password need to be judged, in order to prevent SQL injection attacks, we first through the user name query stored in the data does not exist, and then to match the password. Of course, the password should actually be encrypted, and here for convenience, the password is not encrypted. In addition, in addition to the user name password, there is a verification code, so also need to set the verification code, such as how many Ah, there is no background picture, width and high length ah and so on. See the code below for details.

To do this, we wrote a Model that internally has a function to query whether the user name password is correct.

<?phpnamespace Model; UseThink\model;classManagermodelextendsModel {functionChecknamepwd ($name,$pwd){    $info=$this-Getbymg_name ($name); Var_dump($info); if($info!=NULL){      if($info[' Mg_pwd ']!=$pwd){        returnflase; }Else{        return $info; }    }Else{      return false; }  }}

At the same time there is a corresponding controller, mainly on the verification code settings and testing, as well as the user name and password judgment, the correct judgment may be jump and so on.

<?phpnamespace Admin\controller; UseThink\controller;classManagercontrollerextendscontroller{functionLogin () {//Var_dump ($_post);    if(!Empty($_post)){      //first check that the verification code is correct      $verify=New\think\verify (); if(!$verify, check ($_post[' Captcha '])){        Echo"Captcha Error"; }Else{        //make a special method to verify in Model        Echo"343"; $user=New\model\managermodel (); //the function in Model is called here to check        $rst=$user-Checknamepwd ($_post["Mg_username"],$_post["Mg_password"]); if($rst===flase) {          Echo"User name or password error"; }Else{          //Save in sessionSession (' Mg_username ',$rst[' Mg_name ']); Session (' mg_id ',$rst[mg_id]); //session (null); clear all;          $thisRedirect (' Index/index ',Array(' id ' =>100, name=> ' Tom ')); }      }    }    $this-display (); }  //Verification Code Settings  functionverifyimg () {$config=Array(      ' USEIMGBG ' =false,//use a background image' FontSize ' = 16,//captcha font size (px)' Imageh ' = 40,//Verification Code Picture height' Imagew ' = 100,//Verification Code Picture width' Length ' = 1,//Verify number of code bits    ); $verify=New\think\verify ($config); Var_dump($verify-entry ()); }}

13, Cache settings. Using the principle, for some commonly used data, we can cache, but also for the database changes in the data is not fast, can also be cached. First gets from the database and then into the cache, setting the validity period. The next time you visit, you don't have to query from the database and get it directly from the cache.

functionY1 () {Var_dump($this-y2 ()); }  functiony2 () {//called by another method to get the specified information    $info= S (' Goods_info '); if($info){      return $info; }Else{      //get data from a database without a cache      $dt= "Apple5s". Time(); S (' Goods_info ',$dt, 10); return $dt; }  }

Y2 is a generic method, and the reason why it is not written directly in Y1 is that sometimes it may be more than just Y1 needed, and other functions will be used, so we put it in Y2.

14, attachment upload.

15. Role-based access rights control

Partition permissions on a role

Employees directly face the role, role directly facing specific operational rights

A new employee only needs to determine its role, and his authority is determined by the role

Employee -------------Role --------------operation permissions

Permission assignment is now very easy and very scientific

The new employee entry company only needs to divide its role, its authority has the role to determine

Sw_manager Background User Administrator table

Sw_role Role Table

Sw_auth Specific Permission table

1. Data simulation

2. Development of relevant programs through the simulation of good data

A) User login system, the left Display permission button will be displayed according to the specific role

b) display the corresponding permission information on the left (admin/index/left)

    • User Login System Display permission information $_session[' mg_id ']
    • Get role role_id information based on session information
    • get permission list IDs information based on role ID information
    • Query the specific permission information according to the permission IDs information and then display

While assigning permissions to users and roles, it also enables the display of corresponding permissions.

However, users can access other permissions by displaying the modified URL address

Solve:

Every time a user accesses a controller and method, it needs to be compared with the AC information inside the corresponding role.

AC information inside this controller and method is allowed to access, otherwise prohibit access

Technical Angle:

Determines whether the current controller and method allow role access while each controller is instantiated for execution

For the convenience of the program maintenance, to the general controller to make the parent class controller, in the parent class controller construction method inside the controller and method of filtering work.

The general Controller introduces the new parent class Admincontroller,

1. implement each Access controller and operation method filter inside The new parent class controller Admincontroller

A) Specific restrictions in three different situations

    • The custom method does not have permission restrictions (Index/head left right manager/login)
    • System administrators do not have permission restrictions
    • The permissions that the current user allows access to are the permissions that the role corresponds to

16. Role Maintenance

Admin/role/showlist displaying role information

Collect information from the permission forms of many check boxes and save them in the data table of the role

When setting permissions, display the existing permissions (check box is selected)

The current role knows that the current role corresponds to the permissions IDs also know, you can also change IDs from string to array

When traversing permissions, the ID is compared with the IDs array to determine if it is an element, and then set the Checked property In_array ().

1. Create a form to display specific assigned permission information

2. Collect permission information to be stored in the Rolemodel model

3. When assigning permissions, the existing permissions, check boxes need to be selected

4. The form that displays the assigned permissions information corresponds to the Controller section:

17. Rights Maintenance

Show, add permissions

Authcontroller

function Showlist ()

function Add ()

1. Display permission information, and the name to set the indentation relationship words

2. Add Permissions

3. Optimization

① Add permission parent displays only level=0 or level=1

GetInfo Pass different parameters, the information obtained is also slightly different

② assigning permissions to a role, displaying level three permissions

18, "Administrator list implementation"

Manager/showlist

Summarize:

1. Assigning permissions by role Maintenance

2. Maintain permission data and add permissions

A) Auth_path

b) Help us sort the data to show

c) Full path: The parent's full path and its own ID combined information

d) Auth_level

e) Basic:

3. Maintaining administrator data , role information ({html_options})

Thinkphp Project Summary

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.