Phalcon VS Spring 用法对照手册

Phalcon VS Spring

摘要

Phalcon VS Spring 用法对照表


目录

  • 1. Install
    • 1.1. Phalcon
    • 1.2. Spring
  • 2. Project initialization
    • 2.1. Phalcon
    • 2.2. Spring
  • 3. Controller
    • 3.4.1. Phalcon
    • 3.4.2. Spring
    • 3.3.1. Phalcon
    • 3.3.2. Spring
    • 3.2.1. Phalcon
    • 3.2.2. Spring
    • 3.1.1. Phalcon
    • 3.1.2. Spring
    • 3.1. welcome
    • 3.2. pathinfo
    • 3.3. HTTP Get
    • 3.4. HTTP Post
  • 4. View
    • 4.4.1. Phalcon
    • 4.4.2. Spring
    • 4.3.1. Phalcon
    • 4.3.2. Spring
    • 4.2.1. Phalcon
    • 4.2.2. Spring
    • 4.1.1. Phalcon
    • 4.1.2. Spring
    • 4.1. Variable
    • 4.2. Array
    • 4.3. Map or Hashmap
    • 4.4. From
  • 5. Model
    • 5.1. Phalcon
    • 5.2. MyBatis
  • 6. Cache
    • 6.3.1.
    • 6.3.2.
    • 6.2.1. Phalcon
    • 6.2.2. MyBatis
    • 6.1.1. Phalcon
    • 6.1.2. Spring
    • 6.1. Redis
    • 6.2. Model + Cache
    • 6.3. Phalcon vs Ehcache
  • 7. JSON Data
    • 7.1. Phalcon
    • 7.2. Spring
  • 8. Message Queue
    • 8.1. Phalcon
    • 8.2. Spring

1. Install

1.1. Phalcon

FYI https://github.com/oscm/shell/blob/master/lang/php/pecl/phalcon.sh

You need to install with compiler, make tools.

			#!/bin/sh
cd /usr/local/src/

git clone --depth=1 git://github.com/phalcon/cphalcon.git
cd cphalcon/build
./install

cat > /srv/php/etc/conf.d/phalcon.ini <<EOF
extension=phalcon.so
EOF			

1.2. Spring

You just only create a file as pom.xml, the maven will be fetch them.

			<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>Spring</groupId>
	<artifactId>Spring</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>

	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.1.1.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>4.1.1.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.1.1.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>4.1.1.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>

		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.1.3</version>
		</dependency>
	</dependencies>

	<build>
		<sourceDirectory>src</sourceDirectory>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.3</version>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.6</version>
				<configuration>
					<warSourceDirectory>WebContent</warSourceDirectory>
					<failOnMissingWebXml>false</failOnMissingWebXml>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>			

2. Project initialization

2.1. Phalcon

			<?php
use Phalcon\Mvc\Dispatcher;
use Phalcon\Events\Manager as EventsManager;
use Phalcon\Logger;
try {


	/**
	 * Read the configuration
	 */
	$config = include(__DIR__."/../app/config/config.php");

	$loader = new \Phalcon\Loader();

	/**
	 * We're a registering a set of directories taken from the configuration file
	 */
	$loader->registerDirs(
		array(
			$config->application->controllersDir,
			$config->application->modelsDir,
            $config->application->formsDir,
            $config->application->imagesDir,
		)
	)->register();

	$loader->registerNamespaces(array(
			'Phalcon' => __DIR__.'/../../Library/Phalcon/'
	));

	$loader->register();

	/**
	 * The FactoryDefault Dependency Injector automatically register the right services providing a full stack framework
	 */
	$di = new \Phalcon\DI\FactoryDefault();

        $di->set('dispatcher', function() use ($di) {

                $eventsManager = new EventsManager;
                $dispatcher = new Dispatcher;
                $dispatcher->setEventsManager($eventsManager);

                return $dispatcher;
        });

	/**
	 * The URL component is used to generate all kind of urls in the application
	 */
	$di->set('url', function() use ($config) {
		$url = new \Phalcon\Mvc\Url();
		$url->setBaseUri($config->application->baseUri);
		return $url;
	});

	/**
	 * Setting up the view component
	 */
	$di->set('view', function() use ($config) {
		$view = new \Phalcon\Mvc\View();
		$view->setViewsDir($config->application->viewsDir);
		return $view;
	});

    /**
	 * 数据库加密key
	 */
	$di->set('config', function() use ($config) {
		return $config;
	});


	/**
	 * Database connection is created based in the parameters defined in the configuration file
	 */

	$di->set('db', function() use ($config) {

		return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
			"host" => $config->database->host,
			"username" => $config->database->username,
			"password" => $config->database->password,
			"dbname" => $config->database->dbname
		));
	});

	/**
	 * Start the session the first time some component request the session service
	 */
  	$di->set('session', function() {
  		$session = new \Phalcon\Session\Adapter\Files();
  		$session->start();
  		return $session;
  	});
/*
	$di->set('session', function() use ($config) {
		$session = new Phalcon\Session\Adapter\Redis(array(
				'path' => sprintf("tcp://%s:%s?weight=1",$config->redis->host, $config->redis->port)
		));
		$session->start();
		return $session;
	});
*/
	/**
	 * If the configuration specify the use of metadata adapter use it or use memory otherwise
	 */
	$di->set('modelsMetadata', function() use ($config) {
		if (isset($config->models->metadata)) {
			$metadataAdapter = 'Phalcon\Mvc\Model\Metadata\\'.$config->models->metadata->adapter;
			return new $metadataAdapter();
		} else {
			return new \Phalcon\Mvc\Model\Metadata\Memory();
		}
	});

	/**
	 * If the configuration specify the use of metadata adapter use it or use memory otherwise
	 */
	$di->set('modelsManager', function() {
		return new Phalcon\Mvc\Model\Manager();
	});


	/**
	 * Handle the request
	 */
	$application = new \Phalcon\Mvc\Application();
	$application->setDI($di);
	echo $application->handle()->getContent();

} catch (Phalcon\Exception $e) {
	echo $e->getMessage();
} catch (PDOException $e){
	echo $e->getMessage();
}			

2.2. Spring

WebContent\WEB-INF

			<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Spring</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
        <servlet-name>netkiller</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>netkiller</servlet-name>
        <url-pattern>/welcome.jsp</url-pattern>
        <url-pattern>/welcome.html</url-pattern>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

</web-app>			

netkiller-servlet.xml

			<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

	<context:component-scan base-package="cn.netkiller.controller" />

	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.UrlBasedViewResolver">
		<property name="viewClass"
			value="org.springframework.web.servlet.view.JstlView" />
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>

</beans>			

3. Controller

3.1. welcome

3.1.1. Phalcon

				<?php

class IndexController extends ControllerBase
{

    public function indexAction()
    {

    }
    public function welcomeAction()
    {
    	$message = "Helloworld!!!";
		$this->view->setVar('message',$message);
    }

}				

3.1.2. Spring

				package cn.netkiller.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class Welcome {

	@RequestMapping("/welcome")
	public ModelAndView helloWorld() {

		String message = "Helloworld!!!";
		return new ModelAndView("welcome", "message", message);
	}
}				

3.2. pathinfo

http://www.netkiller.cn/news/list/100.html

http://www.netkiller.cn/news/detail/100/1000.html

3.2.1. Phalcon

				<?php

class NewsController extends \Phalcon\Mvc\Controller
{


    public function listAction($category_id)
    {
		$this->view->setVar('category_id',$category_id);
    }
    public function detailAction($category_id, $article_id)
    {
		$this->view->setVar('category_id',$category_id);
		$this->view->setVar('article_id',$article_id);
    }

}				

3.2.2. Spring

				package cn.netkiller.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class Pathinfo {
	@RequestMapping("/news/list/{category_id}")
	public ModelAndView urlTestId(@PathVariable String category_id) {

		return new ModelAndView("news/list", "category_id", category_id);
	}

	@RequestMapping("/news/detail/{category_id}/{article_id}")
	public ModelAndView urlTestId(@PathVariable String category_id, @PathVariable String article_id) {

		ModelMap model = new ModelMap();

		model.addAttribute("category_id", category_id);
		model.addAttribute("article_id", article_id);

		return new ModelAndView("news/detail", model);
	}
}				

3.3. HTTP Get

http://www.netkiller.cn/member/login?email=netkiller@msn.com

3.3.1. Phalcon

				<?php

class MemberController extends ControllerBase
{

    public function loginAction()
    {
		echo "email=" . $request->get("email");
    }

}				

3.3.2. Spring

				package cn.netkiller.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class Welcome {

	@RequestMapping("/member/login")
	@ResponseBody
	public String getEmailWithRequestParam(@RequestParam("email") String email) {
	    return "email=" + email;
	}

}				

如果参数很多写起来就非常辛苦

				package cn.netkiller.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class Welcome {

	@RequestMapping("/member/login")
	@ResponseBody
	public String getEmailWithRequestParam(
		@RequestParam("email") String email
		@RequestParam("password") String password
		...
		...
		@RequestParam("ext") String ext
	) {
	    ...
	    ...
	    ...
	}

}				

3.4. HTTP Post

3.4.1. Phalcon

				<?php

class MemberController extends ControllerBase
{

    public function loginAction()
    {
		echo "email=" . $request->getPost("email");
    }

}				

原文发布于微信公众号 - Netkiller(netkiller-ebook)

原文发表时间:2015-11-25

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏小白鼠

Mybatis

从xml配置文件中读取配置,然后通过SqlSessionFactoryBuilder构建SqlSessionFactory实例(建造者模式)。SqlSessio...

1153
来自专栏小筱月

spring+springMVC 整合 MongoDB 实现注册登录

发现一入手 MongoDB,便无法脱离,简要说一下,MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的...

642
来自专栏wannshan(javaer,RPC)

dubbo监控机制之监控中心实现分析

这里的监控中心以dubbo-ops\dubbo-monitor-simple项目说 总的来说是监控中心启动一个sevlet容器,通过web页面向用户多维度的展...

3626
来自专栏cmazxiaoma的架构师之路

SpringBoot之路(二)之Web进阶

1484
来自专栏用户画像

spring定时任务之quartz

Spring整合Quartz实现定时任务步骤很简单,大致需要经过如下几步:创建任务(Job)、配置JobDetail、配置触发器(Trigger)、配置Sche...

411
来自专栏Java架构沉思录

Spring Boot是如何实现自动配置的

Spring Boot 是 Spring 旗下众多的子项目之一,其理念是约定优于配置,它通过实现了自动配置(大多数用户平时习惯设置的配置作为默认配置)的功能来为...

853
来自专栏微信公众号:Java团长

Spring Boot 自动配置的“魔法”是如何实现的?

Spring Boot是Spring旗下众多的子项目之一,其理念是约定优于配置,它通过实现了自动配置(大多数用户平时习惯设置的配置作为默认配置)的功能来为用户快...

882
来自专栏芋道源码1024

熔断器 Hystrix 源码解析 —— 断路器 HystrixCircuitBreaker

本文主要基于 Hystrix 1.5.X 版本 1. 概述 2. HystrixCircuitBreaker 3. HystrixCircuitBreaker....

4117
来自专栏精讲JAVA

Spring Boot 自动配置的 “魔法” 是如何实现的?

Spring Boot是Spring旗下众多的子项目之一,其理念是约定优于配置,它通过实现了自动配置(大多数用户平时习惯设置的配置作为默认配置)的功能来为用户快...

802
来自专栏码匠的流水账

聊聊spring cloud gateway的RedisRateLimiter

本文主要研究下spring cloud gateway的RedisRateLimiter

472

扫描关注云+社区