just do it

基于Yii框架的简单api开发

1.根据需求设计API框架

从缓冲中获取最新招聘信息

名称:home_page.get_new_job_from_cache
输入:无
输出:结果集(招聘id-id、公司id-company_id、公司-company_name、职位-career、性质-type、地点-region、工作经验-experience、学历要求-degree、发布时间-created_time);

从缓冲中删除最新招聘信息

名称:home_page.del_new_job_from_cache
输入:无
输出:无


2.根据设计结果、API约束规范、数据字典实现API

编写规范

程序中可以用api('api名称', 参数1, 参数2, …)这种形式调用其他api
一个api文件只能包含一个api方法(注意:api内部调用的方法也能放在此文件中)
api名称:全部用小写,字之间用“_”分隔,模块用“.”分隔,比如,db.get_one_row
api文件:名称中的“.”前的变为文件夹,最后的是文件名,api名称中将“.”替换为“_”作为函数名,以db.get_one_row为例:
db/get_one_row.php:

function db_get_one_row($a_sql)
{
...
}

API实现:两个function分别写在各自方名命名的php文件中,get_new_job_from_cache.php,del_new_job_from_cache.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
function home_page_get_new_job_from_cache()
{
	$key = 'home_page.get_new_job_from_cache';
	$value = Yii::app()->cache->get($key);
 
	if ($value == false)
	{
		$sql = "SELECT 
			j.id, 
			j.company_id as company_id, 
			c.name as company_name, 
			t.tag_name as career,
			CASE j.type
			WHEN 1 THEN '全职'
			WHEN 2 THEN '兼职'
			WHEN 3 THEN '实习'
			WHEN 4 THEN '全职或兼职'
			WHEN 5 THEN '合作' end as type,
			a.tag_name as region, 
			CASE j.experience
			WHEN 1 THEN '无要求'
			WHEN 2 THEN '在读学生'
			WHEN 3 THEN '应届毕业生'
			WHEN 4 THEN '一年以上'
			WHEN 5 THEN '二年以上'
			WHEN 6 THEN '三年以上'
			WHEN 7 THEN '五年以上'
			WHEN 8 THEN '八年以上'
			WHEN 9 THEN '十年以上' end as experience,
			CASE j.degree
			WHEN 1 THEN '无要求'
			WHEN 2 THEN '专科'
			WHEN 3 THEN '本科'
			WHEN 4 THEN '硕士'
			WHEN 5 THEN '博士' end as degree,
			j.created_time
			FROM {{job}} as j
			LEFT JOIN {{company_info}} as c ON j.company_id = c.id 
			LEFT JOIN {{tag_info}} as t ON j.career_id = t.id 
			LEFT JOIN {{tag_info}} as a ON j.region_id = a.id 
			WHERE j.status = 1 order by j.created_time desc limit 0, 10";
 
		$value = Yii::app()->db->createCommand($sql)->queryAll();
 
		Yii::app()->cache->set($key, $value);
	}
 
	return $value;
 
}
function home_page_del_new_job_from_cache()
{
	$key = 'home_page.get_new_job_from_cache';
	Yii::app()->cache->delete($key);
 
}

3.调试正确后前端实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<div style="margin-top:10px;">
<strong>最新招聘</strong>
<span style=" float:right"><a href="<?php echo Yii::app()->createUrl("job/recruitment/index");?>" >查看更多招聘</a></span>
</div>
<?php
$dataProvider=new CArrayDataProvider(api('home_page.get_new_job_from_cache'));
 
$this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'unpaida-fee-grid',
	'htmlOptions' => array(
		'style' => 'margin-top:5px;padding-top:0px;border-top:1px #ccc solid;'
	),
	'summaryText'=>'',
	'dataProvider' => $dataProvider,
	'columns' => array(
 
 
	   array(
 
		   'class'=>'CLinkColumn',
		   'header'=>'公司',
		   'labelExpression'=>'CHtml::encode($data["company_name"])',
		   'urlExpression'=>'Yii::app()->createUrl("/job/recruitment/viewcompany/id/".$data["company_id"])',
	   ),
 
	   array( 
		   'class'=>'CLinkColumn',
		   'header'=>'职位',
		   'labelExpression'=>'CHtml::encode($data["career"])',
		   'urlExpression'=>'Yii::app()->createUrl("/job/recruitment/viewrecruitment/id/".$data["id"])',
	   ),
 
		array(
			'name' => '性质', 
			'value' => '$data["type"]',
	   ),
	   	array(
			'name' => '地点', 
			'value' => '$data["region"]',
	   ),
	   array(
			'name' => '工作经验', 
			'value' => '$data["experience"]',
	   ),
		array(
			'name' => '学历', 
			'value' => '$data["degree"]',
	   ),
		array(
			'name' => '发布时间', 
			'value' => '$data["created_time"]',
	   ),
 
 
 
	),
));
?>
点赞