2、为了让你更了解YY框架的原理和构成,我们先介绍几个简单的示例再介绍开发管理中心的使用,虽然开发管理中心会帮助我们省去一些敲代码的工作量,但是作为初学者,还是建议你一步一步的往下看。 Nginx的配置:
linux下我们更常用nginx来代替apache完成页面请求转发的工作,下面是在一个简单的nginx配置示例:
?? user www www; ??
?? worker_processes 1; ??
?? error_log /home/wwwlogs/nginx_error.log crit; ??
?? pid /usr/local/nginx/logs/nginx.pid; ??
?? #Specifies the value for maximum file descriptors that can be opened by this process.
?? worker_rlimit_nofile 51200; ??
?? events ?? {
?? use epoll;
?? worker_connections 51200; ?? } ?? ?? http ?? {
?? include mime.types;
?? default_type application/octet-stream; ??
?? server_names_hash_bucket_size 128; ?? client_header_buffer_size 32k; ?? large_client_header_buffers 4 32k; ?? client_max_body_size 50m; ??
?? sendfile on; ?? tcp_nopush on; ??
?? keepalive_timeout 60; ??
?? tcp_nodelay on; ??
?? fastcgi_connect_timeout 300;
?? fastcgi_send_timeout 300; ?? fastcgi_read_timeout 300; ?? fastcgi_buffer_size 64k; ?? fastcgi_buffers 4 64k;
?? fastcgi_busy_buffers_size 128k; ?? fastcgi_temp_file_write_size 256k; ??
?? gzip on;
?? gzip_min_length 1k; ?? gzip_buffers 4 16k; ?? gzip_http_version 1.0; ?? gzip_comp_level 2;
?? gzip_types text/plain application/x-javascript text/css application/xml; ?? gzip_vary on; ??
?? #limit_zone crawler $binary_remote_addr 10m; ??
?? #log format
?? log_format access '$remote_addr - $remote_user [$time_local] \
?? '$status $body_bytes_sent \?? '\?? server ?? {
?? listen 80;
?? server_name www.yyuc.net;
?? index index.html index.htm index.php; ?? root /home/test/pub; ??
?? location / {
?? if (!-e $request_filename) { ?? rewrite ^/(.*)$ /index.php last; ?? } ?? } ??
?? location ~ .*\\.(php|php5)?$ ?? {
?? try_files $uri =404;
?? fastcgi_pass unix:/tmp/php-cgi.sock; ?? fastcgi_index index.php; ?? include fcgi.conf; ?? } ??
?? location /status { ?? stub_status on; ?? access_log off; ?? } ??
?? location ~ .*\\.(gif|jpg|jpeg|png|bmp|swf)$ ?? {
???expires 30d; ???} ???
???location ~ .*\\.(js|css)?$ ???{
???expires 12h; ???} ???
???access_log /home/wwwlogs/access.log access; ???} ???}
hello world
功能需求:
输入地址http://www.yyuc.net/demo/hello.html,页面显示hello wolrd字符。 通过阅读和学习通用简单路由,你会知道这个请求页面的控制器文件是: ???controller/demo/hello.php。
在controller文件夹下建立demo目录和hello.php文件。
方式1:
编辑hello.php 代码如下:
???
???Page::ignore_view();
???Response::write(\????>
其中page类是对页面的一个封装类,里面有一系列的静态参数和方法供控制器直接修改和调用。
page::$need_view 默认为 true,执行完这个php文件之后框架会继续加载它对
应的视图文件来执行,Page::ignore_view()将其设为 false则执行完php文件后就不再寻找视图文件了。
Response::write 方法是向客户端进行文本输出,执行后立即退出脚本。
方式2:
hello.php 文件不写任何代码,可以建立空文件:controller/demo/hello.php。 建立文件:view/default/demo/hello.html hello.html内容为: ???
hello World
由此可见,如果没有执行Page::ignore_view(),框架执行了hello.php文件之后,控制器会自动寻找视图文件hello.html文件加载执行。
方式3:
修改配置文件conf.php将$auto_find_view改为true。 ???/**是否开启无控制器时自动寻找对应视图~默认:false*/ ???public static $auto_find_view = true;
无需创建控制器文件直接建立文件:view/default/demo/hello.html 内容为:
???
hello World
配置数据库
配置数据库连接
这只是一个标准示例,实际开发中并不一定一定按照示例的方式进行。 框架的主配置文件是/yyuc/conf.php,它是一个被封装好的静类文件,有关数据库的配置如下:
???/**数据库地址~*/
???public static $db_host = \???/**数据库端口~*/
???public static $db_port = \???/**数据库名~*/
???public static $db_dbname = \
???/**数据库用户名~*/
???public static $db_username = \???/**数据库密码~*/
???public static $db_password = \???/**数据库表前缀~*/
???public static $db_tablePrefix = \
建立数据库和表
可以通过自己常用的mysql管理工具完成这一工作。这里我们建立的表名称是:qq_notes DDl语句如下:
???CREATE TABLE `qq_notes` (
???`id` int(11) NOT NULL auto_increment COMMENT '主键', ???`author` varchar(255) default NULL COMMENT '作者', ???`theme` enum('Arts','Emotion','Humanities','Technology') default NULL COMMENT '主题:艺术,情感,人文,科技',
???`title` varchar(255) default '新建题目' COMMENT '标题', ???`content` text COMMENT '内容',
???`bepublished` tinyint(1) default NULL COMMENT '是否发布', ???`postdate` int(11) default NULL COMMENT '提交时间', ???PRIMARY KEY (`id`)
???) ENGINE=InnoDB DEFAULT CHARSET=utf8; 关于数据表的创建有以下几点说明:
1、像Rails一样,如果要通过面向对象的方式操作,YY框架要求如果表要依据Model类操作必须有一个名为\int类型的自增主键。
2、建议所有字段都要有注记,一是易于表的维护和管理,二是在自动代码生成过程中减少生成后的代码的后期修改量,实现根据注记内容动态修改字段描述的功能。
3、对于布尔类型,用tinyint(1)表示,0代表:否,1代表:是。 下面两条可以根据开发者的喜好采用:
1、对于日期类型和日期时间类型,用int(9)表示,php开发中因为time()和date()方法的存在大多数开发者喜欢用数据库的int类型表示时间而放弃了date和datetime。
2、根据喜好可以对于枚举类型,通常页面上会用下拉框或单选按钮的形式与其关联,所以在枚举字段的注记中采用\ 进行描