Há três Actions no IndexController para demonstrar a utilização da autenticação HMAC. A única alteração necessária no Controller é, quando necessário, para obter a identificação do cliente da API.
Veja como ativar autenticação HMAC em uma aplicação criada a partir do ZF2 Skeleton seguindo as etapas abaixo.
Configure a dependência em composer.json
:
(...)
"minimum-stability": "dev",
"prefer-stable": true,
"repositories": [
{
"type": "git",
"url": "https://github.com/reinaldoborges/rb-sphinx-hmac-zf2.git"
},
{
"type": "git",
"url": "https://github.com/reinaldoborges/rb-sphinx-hmac.git"
}
],
"require": {
"php": ">=5.5",
"rb/sphinx-hmac-zf2": "dev-develop"
}
(...)
Crie uma Abstract Factory para instanciar os objetos HMAC (com suas configurações) para cada situação. Neste exemplo, criamos um módulo Rbhmac para isso. O arquivo module.config.php
registra essa Factory:
return array(
'service_manager' => array (
'abstract_factories' => array (
'Rbhmac\HMACAbstractFactory'
)
)
);
Carregue os módulos Sphinx HMAC ZF2 e o criado no exemplo que faz a configuração no arquivo application.config.php
:
(...)
'modules' => array(
'Application',
'RB\\Sphinx\\Hmac\\Zend',
'Rbhmac'
),
(...)
Configure quais Controllers e Actions irão exigir autenticação HMAC, e indique que tipo de autenticação cada um utilizará. Nesse exemplo usamos o arquivo config/autoload/local.php
para isso:
(...)
'Application\Controller\Index' => array(
'actions' => array(
'index' => false,
'header' => array(
'selector' => 'HMAC',
'adapter' => 'HMACHeaderAdapter'
),
(...)
Há um exemplo mais detalhado dessa configuração no módulo em vendor/rb/sphinx-hmac-zf2/config/local.php.dist
This is a simple, skeleton application using the ZF2 MVC layer and module systems. This application is meant to be used as a starting place for those looking to get their feet wet with ZF2.
The easiest way to create a new ZF2 project is to use Composer. If you don't have it already installed, then please install as per the documentation.
Create your new ZF2 project:
composer create-project -n -sdev zendframework/skeleton-application path/to/install
If you don't have composer installed globally then another way to create a new ZF2 project is to download the tarball and install it:
-
Download the tarball, extract it and then install the dependencies with a locally installed Composer:
cd my/project/dir curl -#L https://github.com/zendframework/ZendSkeletonApplication/tarball/master | tar xz --strip-components=1
-
Download composer into your project directory and install the dependencies:
curl -s https://getcomposer.org/installer | php php composer.phar install
If you don't have access to curl, then install Composer into your project as per the documentation.
The simplest way to get started if you are using PHP 5.4 or above is to start the internal PHP cli-server in the root directory:
php -S 0.0.0.0:8080 -t public/ public/index.php
This will start the cli-server on port 8080, and bind it to all network interfaces.
Note: The built-in CLI server is for development only.
This project supports a basic Vagrant configuration with an inline shell provisioner to run the Skeleton Application in a VirtualBox.
-
Run vagrant up command
vagrant up
-
Visit http://localhost:8085 in your browser
Look in Vagrantfile for configuration details.
To setup apache, setup a virtual host to point to the public/ directory of the project and you should be ready to go! It should look something like below:
<VirtualHost *:80>
ServerName zf2-app.localhost
DocumentRoot /path/to/zf2-app/public
<Directory /path/to/zf2-app/public>
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
<IfModule mod_authz_core.c>
Require all granted
</IfModule>
</Directory>
</VirtualHost>
To setup nginx, open your /path/to/nginx/nginx.conf
and add an
include directive below
into http
block if it does not already exist:
http {
# ...
include sites-enabled/*.conf;
}
Create a virtual host configuration file for your project under /path/to/nginx/sites-enabled/zf2-app.localhost.conf
it should look something like below:
server {
listen 80;
server_name zf2-app.localhost;
root /path/to/zf2-app/public;
location / {
index index.php;
try_files $uri $uri/ @php;
}
location @php {
# Pass the PHP requests to FastCGI server (php-fpm) on 127.0.0.1:9000
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME /path/to/zf2-app/public/index.php;
include fastcgi_params;
}
}
Restart the nginx, now you should be ready to go!