为什么80%的码农都做不了架构师?>>>   

#Create a restful application with AngularJS and CakePHP (I)

#Prepare project skeleton

There are several solutions provided in the CakePHP documents to create a new CakePHP project from scratch.

  • clone the cakephp from github, and work on it.
  • use bake command line from the shared Cake distribution to create a new project.

In the advanced section, the official guide also motioned how to create a CakePHP project via bake console and manage project dependencies via Composer.

Let's create a CakePHP project step by step.

Bake project skeleton

  1. Create a folder for the new project, eg. angularjs-cakephp-sample.

  2. In the angularjs-cakephp-sample root, create a folder named Vendor.

  3. Get a copy of composer.phar from Composer, put it in the root folder of angularjs-cakephp-sample.

  4. Create a text file named composer.json, the content is the following.

<pre> { "name": "example-app", "repositories": [ { "type": "pear", "url": "http://pear.cakephp.org" } ], "require": { "pear-cakephp/cakephp": ">=2.4.0", "cakephp/debug_kit": "2.2.*", "cakephp/localized": "2.1.x-dev" }, "config": { "vendor-dir": "Vendor/" } } </pre>

Put it into the root folder of angularjs-cakephp-sample.

Now the root folder structure looks like the following.

<pre> angularjs-cakephp-sample /Vendor composer.json composer.phar </pre>

  1. Run php composer.phar install in the project root folder. After it is done, you will get a copy of the latest CakePHP, and it is extracted into the Vendor folder. Now the folder should look like.

<pre> angularjs-cakephp-sample /Vendor /pear-pear.cakephp.org /CakePHP composer.json composer.phar </pre>

  1. Create the new project skeleton using bake command line in the CakePHP dependency(under Vendor).

<pre> Vendor\pear-pear.cakephp.org\CakePHP\bin\cake.bat bake project --app &lt;path of angularjs-cakephp-sample> </pre>

You maybe notice there is a copy of cake under Vendor\bin, but unfortunately it does not work in the current stable version(2.4.2) as described in the document.

Due to this issue, under windows, I have to use the above command line to create the project. And this issue will be fixed in the further 2.4.3.

Configure Apache for this project

I assume you have installed a PHP runtime environment for this project. PHP is ready for all popular Linux distributions. I am using WAMP 2.4 under windows.

###Enable mod_rewrite

mod_rewrite is required for CakePHP application. You have to enable it in the Apache configuration file.

Find the following line in the <apache>/conf/httpd.conf file.

<pre> LoadModule rewrite_module modules/mod_rewrite.so </pre>

Make sure it is not commented.

WAMP provides a context menu to configure the modules in Apache. You can click the mod_rewrite menu item to enable or disable.

After the configuration is saved, do not forget to restart the Apache server.

###Configure DocumentRoot

In order to run the project on Apache, you need extra configuration.

The simplest way is set the DocumentRoot to the project path.

<pre> DocumentRoot "&lt;project path>" </pre>

Alternatively, you can use a standalone host settings.

Create a angularjs-cakephp-sample.conf in the <WAMP>/hosts.

<pre> &lt;VirtualHost *:80> ServerName localhost DocumentRoot E:/MyWorks/hantsy-labs/angularjs-cakephp-sample/webroot &lt;Directory E:/MyWorks/hantsy-labs/angularjs-cakephp-sample/webroot> DirectoryIndex index.php Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny Allow from all &lt;/Directory> &lt;/VirtualHost> </pre>

Now you can access the project via http://localhost.

If you want to use a context path for this project, you can use alias configuration or enable user_dir module support in Apache configuration.

Thus you can access the project via url http://localhost/angularjs-cakephp-sample, or http://localhost/~<yourname>/angularjs-cakephp-sample, but you have to change the default configuration in the .htaccess under project root and webroot, add a RewriteBase line, the value is the context path of the project.

<pre> &lt;IfModule mod_rewrite.c> RewriteEngine on RewriteBase /angularjs-cakephp-sample RewriteRule ^$ webroot/ [L] RewriteRule (.*) webroot/$1 [L] &lt;/IfModule> </pre>

Modify the source codes

In order to run the project, you should modify the generated sources slightly.

###Modify classloader

In the CakePHP document, there are some content to describe how to modify the CakePHP classloader and make it compatible with Composer generated classloader.

Open Config/bootstrap.php file, append the following content.

<pre> // Load composer autoload. require APP . '/Vendor/autoload.php'; // Remove and re-prepend CakePHP's autoloader as composer thinks it is the most important. // See https://github.com/composer/composer/commit/c80cb76b9b5082ecc3e5b53b1050f76bb27b127b spl_autoload_unregister(array('App', 'load')); spl_autoload_register(array('App', 'load'), true, true); </pre>

Define CAKE_CORE_INCLUDE_PATH

In the webroot/index.php, find the CAKE_CORE_INCLUDE_PATH constant definition, and replace it with the following content.

<pre> define( 'CAKE_CORE_INCLUDE_PATH', ROOT . DS . APP_DIR . '/Vendor/pear-pear.cakephp.org/CakePHP' ); </pre>

Run the project

Start up Apache server, and navigate to http://localhost.

You would see the following screen.

If all configuration are correctly, you could see all the configuration items are displayed as green bars. If there are some error or warning, please go back to the project source codes and fix them firstly.

Summary

The bake command line will generate the session salt and security key, database configuration for you automatically. And Composer provides dependencies for the project. Compare to the PEAR package management, the benefit is obviously. Using Composer you can use a project scoped CakePHP, thus you can use different versions of CakePHP in different projects, and the more powerful feature of Composer is you can upgrade your CakePHP to the new version easily.

转载于:https://my.oschina.net/hantsy/blog/175251

Create a restful application with AngularJS and CakePHP (I)相关推荐

  1. Create a restful app with AngularJS/Grails(4)

    为什么80%的码农都做不了架构师?>>>    #Standalone AngularJS application In the real world applications, i ...

  2. Jersey Restful Application with tomcat

    2019独角兽企业重金招聘Python工程师标准>>> ##1.下载Jersey相关jar包,本示例用的版本是Jersey 2.22.1 ##2.eclipse新建dynamic w ...

  3. opencv学习第6课官方练习实现 Create a Paint application with adjustable colors and brush radius using trackbars

    练习题目来源(网址最下方):https://docs.opencv.org/4.1.0/d9/dc8/tutorial_py_trackbar.html import numpy as np impo ...

  4. Building Huuuuuge Apps with AngularJS

    [2013.01.07] The AngularJS documentation is great for getting started and for looking up specific AP ...

  5. node aws 内存溢出_如何使用Node.js和AWS快速创建无服务器RESTful API

    node aws 内存溢出 by Mark Hopson 马克·霍普森(Mark Hopson) 如何使用Node.js和AWS快速创建无服务器RESTful API (How to quickly ...

  6. 购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(3)--Idetity,OWIN前后端验证

    原文:购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(3)--Idetity,OWIN前后端验证 chsakell分享了前端使用AngularJS,后端使用ASP. ...

  7. RESTful服务的第三部分:HATEOAS和Richardson成熟度模型

    by Sanchit Gera 通过Sanchit Gera RESTful服务的第三部分:HATEOAS和Richardson成熟度模型 (RESTful Services Part III : H ...

  8. AngularJS JetBrains WebStorm简介

    We are going to look at some of the useful developer tools for angularJS development through a serie ...

  9. angularjs绑定属性_AngularJS指令和数据绑定示例教程

    angularjs绑定属性 In the previous post, we have successfully created and run our first angular applicati ...

最新文章

  1. Matlab求方差,均值,均方差,协方差的函数
  2. 反向区域DNS解析服务
  3. Fabonacci三种方法求解C++
  4. 网络基本功系列:细说网络那些事儿
  5. HttpWebRequest与HttpWebResponse进行数据采集时的注意点
  6. 前端HTML以及HTML5(基本标签)
  7. [转]我认识的189名CCIE的薪水情况
  8. 1.3 机器学习基础
  9. android view绘制速度,关于android ui的优化 view 的绘制速度
  10. SpringBoot(三)
  11. 数据库系统概论(第5版)学习笔记第1章 1.1——数据库系统概论
  12. jboss linux环境部署,linux下部署jboss
  13. c语言简单图形库,C语言图形库简单对比及EGE库的安装小手册
  14. 破解获取微信小程序源代码
  15. CSS设置div水平垂直居中
  16. python中shift函数_Python numpy.left_shift函数方法的使用
  17. python struct pack unpack
  18. 《设计模式之禅》目录
  19. springboot实现微信模板消息推送
  20. 双通道(双CPU)服务器主板上内存条的安装方式

热门文章

  1. 视频全程:哈萨比斯首次公开解读AlphaZero
  2. 每日两道前端面试题20190226
  3. [20170816]Join Elimination Bug.txt
  4. 21-hadoop-weibo推送广告
  5. 日本第四次产业革命瞄准物联网
  6. import 和 export -- ES6
  7. Git与GitHub学习笔记(一)如何删除github里面的文件夹?
  8. hihoCoder #1033 : 交错和 [ 数位dp ]
  9. Thin的DateChooser代码学习(getScrollPosition)(原创,转载请声明)
  10. Reporting service 技巧