<?php
HomeDatabaseMysql TutorialCakePHP中文教程2

CakePHP中文教程2

Jun 07, 2016 pm 03:27 PM
cakephprChineseInstallTutorialthird chapter

第三章 安装 CakePHP 1 , Requirements 你必须有一个服务器,具备程序和库来运行 CakePHP 1.1 服务器的要求 1.HTTP 服务端(比如 Apache ),下面的选项要打开: sessions, mod_rewrite( 虽然不是必须但是建议有 ) 2.PH P 4.3.2 或者更高版本, CakePHP 在

第三章 安装CakePHP

1Requirements

    你必须有一个服务器,具备程序和库来运行CakePHP

1.1 服务器的要求   

    1.HTTP服务端(比如Apache),下面的选项要打开:sessions, mod_rewrite(虽然不是必须但是建议有)

    2.PHP 4.3.2或者更高版本,CakePHPPHP45下都能很好工作

    3.数据库引擎(现在已经支持MySQLPostgreSQL and a wrapper for ADODB

 

2.安装CakePHP

2.1 得到最新的稳定版本

    www.cakephp.org 点击Downloads>Release

2.2 解压缩

    放置到根目录,解压缩,有两种安装方式,开发方式安装使得你可以容易的在一个域名下面察看许多CakePHP程序,产品安装方式使得一个域名下面只有一个程序

 

3. 建立CakePHP

    第一种方法在开发环境下比较合适,但是并不安全。第二种方法更安全可以安装在最终产品的环境下

3.1 开发方式安装

为了开发,我们可以将整个Cake的安装目录放到特殊的DocumentRoot下,如下:

 

CakePHP中文教程2/wwwroot/
CakePHP中文教程2    /cake
CakePHP中文教程2        /.htaccess
CakePHP中文教程2        /app/
CakePHP中文教程2        /cake/
CakePHP中文教程2        /index.php
CakePHP中文教程2        /tmp/
CakePHP中文教程2        /vendors
CakePHP中文教程2


这种安装wwwroot当作整个网站的目录,所以你的URL如下显示

CakePHP中文教程2www.example.com/cake/index.php


如果你使用了mod_rewrite, 你的URLs会变成下面的这种形式


4. 配置Apachemod_rewrite

       CakePHP没有mod_rewrite支持时候,我们发现许多用户由于不能完美运行他们的系统而苦恼。下面是一些可能需要尝试并且使之正确运行的事情:

     确定 .htaccessoverride是允许的:在你的httpd.conf中,你需要有一段代码,在你的服务器上每个目录。确定AllowOverride对当前的目录设置成为All

     确定你修改的是系统的httpd.conf,而不是用户或者特定站点的httpd.conf

     在某些情况下,你可能得到一份缺少 .htaccess文件的CakePHP,这可能是因为某些系统把 .开头的文件当作隐藏文件就不拷贝。确定你拷贝的文件来中包含.htaccess

     确定你正确上传mod_rewrite。你可以查看一下 httpd.conf中类似于LoadModule rewrite_module libexec/httpd/mod_rewrite.so AddModulemod_rewrite.c

 

5. 确定CakePHP正确工作

现在,我们实际查看一下。根据你选择不同的安装方式,你应该将你的浏览器的URL指向 http://www.example.com或者http://www.example.com/cake.这时,你会看到CakePHP的默认目录,以及一条数据库连接状态的消息。

恭喜!你现在已经可以创建你的第一个基于Cake的程序了。如果你现在什么都没有看到或者受到一条错误信息,可以到http://wiki.cakephp.org或者#cakephp on irc.freenode.net寻找更多的帮助。

 

第四章  配置

1. 数据库配置

    app/config/database.php文件是数据库配置文件。默认安装后并没有database.php文件,所以你需要拷贝database.php.defaultdatabase.php. 之后,你可以看到如下:

4.1

 

CakePHP中文教程2var $default = array('driver'   => 'mysql',
CakePHP中文教程2                     'connect'  => 'mysql_pconnect',
CakePHP中文教程2                     'host'     => 'localhost',
CakePHP中文教程2                     'login'    => 'user',
CakePHP中文教程2                     'password' => 'password',
CakePHP中文教程2                     'database' => 'project_name' );
CakePHP中文教程2


根据你的数据库连接信息替换上面的配置

CakePHP支持下面的数据库驱动

 

CakePHP中文教程2mysql 
CakePHP中文教程2postgres 
CakePHP中文教程2sqlite 
CakePHP中文教程2pear-drivername (so you might enter pear-mysql, for example) 
CakePHP中文教程2adodb-drivername 
CakePHP中文教程2


$default 连接中的connect key允许你设置是否需要持久连接数据库,请仔细阅读database.php.default文件中的注释,以配置你的数据库连接类型。

你的数据库中的表也应该遵循下面的命名规则:

     cake使用的表名(Table name)应该有英文的复数形式组成,比如users, authors, articles. 注意,对应的model是单数形式

     所有的表 都必须有一个主键叫做 id

     如果你需要关联tables,使用外键比如 article_id 。表的名字是单数,必须是小写,比如id

     另外,最好遵从下面的命名规则以便更方便的使用某些功能

Include a ‘created’

Include a ‘modified’

    你也可能注意到了database.php文件中有一个$test的连接,填上这个配置(或者添加其他类似的配置文件),在你使用的时候将下面的代码

添加到你的一个models

CakePHP中文教程2var $useDbConfig = 'test';


2. 全局配置

    CakePHP的全局配置可以在/app/config/core.php找到。尽管我们确实不喜欢配置文件,但是配置确实很难做。在这个文件中你可以修改一些,每一项设置都可以在core.php的注释中找到。

 

DEBUG: 设置这个变量不同的值可以帮助你在运行文件的时候debug你的程序。江这个值设成不同的非0值,可以使得Cake打印出SQL语句并且阻止程序下一步进行(原文:Specifiying this setting to a non-zero value will force Cake to print out SQL statements and stop flash messages from forwarding.)

CAKE_SESSION_COOKIE:在你的Cake应用中,这个变量可以设置成为保存用户sessioncookie名字

CAKE_SECURITY:这个变量不同的值达标不同的sessions检查等级。Cake创建新的session,删除旧的session都基于这个变量。下面是可能的值:

        high: session十分钟后失效

        medium: session 二十分钟后失效

        low: session 三十分钟后失效

CAKE_SESSION_SAVE: 设置如何保存session。下面是可能的值

        cake: 保存在tmp/目录下

        php: 根据php.ini中的设置保存

        database: 被保存到默认(现在还没有实现,以后继续完善)

 

3.路径设置

Routing是类似于mod_rewrite pared-down pure-PHP(机制),可以帮助将URL影射到 controller/action/params. Cake 添加这个可以帮助我们更好的实现URL转化并使得我们可以脱离mod_rewrite的要求。然而使用mod_rewrite,使得我们的address bar显得更加整洁。

    Routes是映射URLs到特定的controllersactions的独立规则。Routes被配置在app/config/routes.php文件中,设置形式如下:

4.2 Route Pattern

CakePHP中文教程2CakePHP中文教程2$Route->connect (
CakePHP中文教程2    'URL'
, 
CakePHP中文教程2    array('controller'
=>'controllername', 
CakePHP中文教程2    'action'
=>'actionname', 'firstparam')
CakePHP中文教程2)
;
CakePHP中文教程2
?>
CakePHP中文教程2

在这里

URLCakeURL你想要映射的(URL is the Cake URL you wish to map

controllername 是你想要调用的controller的名字

actionname 是你想要调用controlleraction的名字

firstparam是特定的action的第一个参数

 

下面的这个例子将/blog下面的所有URL连接到了BlogController. 默认的actionBlogController::index()

 

4.3 Route Example

 

 

一个URL 比如 /blog/history/05/june 处理如下:

 

4.4 Route Handing in a Controller

CakePHP中文教程2CakePHP中文教程2class BlogController extends AppController
CakePHP中文教程2{
CakePHP中文教程2function history ($year
, $month=null)
CakePHP中文教程2     {
CakePHP中文教程2           //..display appropriate content
CakePHP中文教程2     }
CakePHP中文教程2}
CakePHP中文教程2?>


 

URL中的’history’被匹配到 Blog route中的:action.( The 'history' from the URL was matched by :action from the Blog's route.) URL中被*匹配的元素被传递到活动的controller的处理方法,这里是$year$month。比如这个URL /blog/history/05, 仅仅传递一个参数05history()

    接下来的例子是默认的CakePHP route,指向 PagesController::display(‘home’).其中home是一个view,位于/app/views/pages/home.thtml

 

4.5 Setting the Default Route

CakePHP中文教程2CakePHP中文教程2$Route->connect ('/*', array('controller'=>'Pages', 'action'=>'display', 'home'));
CakePHP中文教程2
?>


CakePHP中文教程2CakePHP中文教程2$Route->connect ('/blog/:action/*', array('controller'=>'Blog', 'action'=>'index'));
CakePHP中文教程2
?>

CakePHP中文教程2http://www.example.com/cake/controller_name/action_name/param1/param2 pattern


产品方式安装

3.2

为了实现产品方式的安装,你必须用于修改服务器上网站根目录的权限,修改根目录,使得整个域像只有一个CakePHP程序

安装后的程序结构如下

CakePHP中文教程2../path_to_cake_install/
CakePHP中文教程2    
/.htaccess
CakePHP中文教程2    
/app/
CakePHP中文教程2        
/config/
CakePHP中文教程2        
/conftrollers
CakePHP中文教程2        
/index.php
CakePHP中文教程2        
/models
CakePHP中文教程2        
/plugins
CakePHP中文教程2        
/views
CakePHP中文教程2        
/webroot -- This should be your new DocumentRoot
CakePHP中文教程2
    /cake/
CakePHP中文教程2    
/index.php
CakePHP中文教程2    
/tmp/
CakePHP中文教程2    
/vendors
CakePHP中文教程2


3.1 建议的httpd.conf

CakePHP中文教程2DocumentRoot /path_to_cake/app/webroot


在这种配置下,webroot就当作了网站的根目录,你的URL就可能像下面这样

CakePHP中文教程2http://www.example.com/


如果你使用了mod_rewrite,你的URL就会类似于下面的

CakePHP中文教程2http://www.example.com/controller_name/action_name/param1/param2 pattern


一些共享主机的用户可能没有权限通过修改http.conf来改变DocumnentRoot来指向他们的产品安装。在这种情况下,用户可以安装下面的方式,修改CakePHP的结构。Cake安装在 /path_to_cake_install,文件目录(不可以修改)指向/public_html

CakePHP中文教程2/path_to_cake_install
CakePHP中文教程2    
/app
CakePHP中文教程2    
/cake
CakePHP中文教程2    
/index.php
CakePHP中文教程2    
/models
CakePHP中文教程2    
/plugins
CakePHP中文教程2    
/views
CakePHP中文教程2    
/webroot
CakePHP中文教程2
/public_html (= contents of /app/webroot)
CakePHP中文教程2    
/.htaccess
CakePHP中文教程2    
/css    
CakePHP中文教程2    
/css.php         
CakePHP中文教程2    
/favicon.ico     
CakePHP中文教程2    
/files           
CakePHP中文教程2    
/img             
CakePHP中文教程2    
/index.php       
CakePHP中文教程2    
/js
CakePHP中文教程2
CakePHP中文教程2


实际上,你将Cake/app/webroot文件夹的内容拷贝到你已经存在的DocumentRoot。为了告诉Cake你的webroot文件在哪里,将下面几行田间到你DocumentRoot文件夹下面的index.php中(针对上面的例子就是/public_html/index.php

CakePHP中文教程2define('ROOT', BASE_DIR.'/path_to_cake_install');
CakePHP中文教程2define ('APP_DIR', 'app');
CakePHP中文教程2define ('WEBROOT_DIR', '/public_html');
CakePHP中文教程2

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Explain the ACID properties (Atomicity, Consistency, Isolation, Durability).Explain the ACID properties (Atomicity, Consistency, Isolation, Durability).Apr 16, 2025 am 12:20 AM

ACID attributes include atomicity, consistency, isolation and durability, and are the cornerstone of database design. 1. Atomicity ensures that the transaction is either completely successful or completely failed. 2. Consistency ensures that the database remains consistent before and after a transaction. 3. Isolation ensures that transactions do not interfere with each other. 4. Persistence ensures that data is permanently saved after transaction submission.

MySQL: Database Management System vs. Programming LanguageMySQL: Database Management System vs. Programming LanguageApr 16, 2025 am 12:19 AM

MySQL is not only a database management system (DBMS) but also closely related to programming languages. 1) As a DBMS, MySQL is used to store, organize and retrieve data, and optimizing indexes can improve query performance. 2) Combining SQL with programming languages, embedded in Python, using ORM tools such as SQLAlchemy can simplify operations. 3) Performance optimization includes indexing, querying, caching, library and table division and transaction management.

MySQL: Managing Data with SQL CommandsMySQL: Managing Data with SQL CommandsApr 16, 2025 am 12:19 AM

MySQL uses SQL commands to manage data. 1. Basic commands include SELECT, INSERT, UPDATE and DELETE. 2. Advanced usage involves JOIN, subquery and aggregate functions. 3. Common errors include syntax, logic and performance issues. 4. Optimization tips include using indexes, avoiding SELECT* and using LIMIT.

MySQL's Purpose: Storing and Managing Data EffectivelyMySQL's Purpose: Storing and Managing Data EffectivelyApr 16, 2025 am 12:16 AM

MySQL is an efficient relational database management system suitable for storing and managing data. Its advantages include high-performance queries, flexible transaction processing and rich data types. In practical applications, MySQL is often used in e-commerce platforms, social networks and content management systems, but attention should be paid to performance optimization, data security and scalability.

SQL and MySQL: Understanding the RelationshipSQL and MySQL: Understanding the RelationshipApr 16, 2025 am 12:14 AM

The relationship between SQL and MySQL is the relationship between standard languages ​​and specific implementations. 1.SQL is a standard language used to manage and operate relational databases, allowing data addition, deletion, modification and query. 2.MySQL is a specific database management system that uses SQL as its operating language and provides efficient data storage and management.

Explain the role of InnoDB redo logs and undo logs.Explain the role of InnoDB redo logs and undo logs.Apr 15, 2025 am 12:16 AM

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?Apr 15, 2025 am 12:15 AM

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

What is the Using temporary status in EXPLAIN and how to avoid it?What is the Using temporary status in EXPLAIN and how to avoid it?Apr 15, 2025 am 12:14 AM

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft