摘要
Phalcon VS Spring 用法对照表
目录
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
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>
<?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();
}
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>
<?php
class IndexController extends ControllerBase
{
public function indexAction()
{
}
public function welcomeAction()
{
$message = "Helloworld!!!";
$this->view->setVar('message',$message);
}
}
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);
}
}
http://www.netkiller.cn/news/list/100.html
http://www.netkiller.cn/news/detail/100/1000.html
<?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);
}
}
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);
}
}
http://www.netkiller.cn/member/login?email=netkiller@msn.com
<?php
class MemberController extends ControllerBase
{
public function loginAction()
{
echo "email=" . $request->get("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) {
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
) {
...
...
...
}
}
<?php
class MemberController extends ControllerBase
{
public function loginAction()
{
echo "email=" . $request->getPost("email");
}
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有