めも。
@ORM\Column(name="status", type="smallint", options={"default" = 1})
めも。
@ORM\Column(name="status", type="smallint", options={"default" = 1})
下記URLを参考にして作ってみた。
https://gist.github.com/romaricdrigon/6291435
■form_theme.html.twig
{# This is an extension of Twig Form Theme #}
{# We redefine form elements, so they are binded with Angular model #}
{% extends "::form_div_layout.html.twig" %}
{# Make data available to current scope as $scope.data - will not work with hidden fields #}
{% block widget_attributes %}
{% spaceless %}
ng-model="$parent.data['{{ full_name }}']" {# we add Angular ng-model #}
id="{{ id }}" name="{{ full_name }}"{% if read_only %} readonly="readonly"{% endif %}{% if disabled %}
disabled="disabled"{% endif %}{% if required %} required="required"{% endif %}{% if max_length %} maxlength="{{
max_length }}"{% endif %}{% if pattern %} pattern="{{ pattern }}"{% endif %}
{% for attrname, attrvalue in attr %}{% if attrname in ['placeholder', 'title'] %}{{ attrname }}="{{ attrvalue|trans({},
translation_domain) }}" {% else %}{{ attrname }}="{{ attrvalue }}" {% endif %}{% endfor %}
{% if value is defined %} ng-init="$parent.data['{{ full_name }}']='{{ value }}'"{% endif %}
{% endspaceless %}
{% endblock widget_attributes %}
{# current controller submit() will called, instead of submitting the form #}
{% block form_start %}
{% spaceless %}
{% set method = method|upper %}
{% if method in ["GET", "POST"] %}
{% set form_method = method %}
{% else %}
{% set form_method = "POST" %}
{% endif %}
<form name="{{ form.vars.name }}" method="{{ form_method|lower }}" {% for attrname, attrvalue in attr %} {{ attrname
}}="{{ attrvalue }}"{% endfor %}{% if multipart %} enctype="multipart/form-data"{% endif %} novalidate>
<input type="hidden" name="_action" ng-init="$parent.action='{{ action }}'" />
{% if form_method != method %}
<input type="hidden" name="_method" value="{{ method }}" />
{% endif %}
{% endspaceless %}
{% endblock form_start %}
※ng-includeで取り込んだテンプレート内ではスコープが再生成されるので$parent.data~としてるところがポイント。
■angular側のフォーム描画部分。
※コントローラーの位置はここでなくても良い
<div
ng-include="formUrl"
ng-controller="FormCntl"
></div>
■コントローラーのJS
coreCtrls.controller('taskEditCtrl', ['$scope', '$routeParams', '$http', '$route', '$templateCache',
function($scope, $routeParams, $http, $route, $templateCache) {
$scope.formUrl = location.pathname + 'api/task/' + $routeParams.id + '/edit';
// フォームデータの入れ物を宣言。
$scope.data = {};
// $scope.actionにポスト先のURLが格納されている。
$scope.submit = function() {
$http.post($scope.action, reconstruction($scope.data)).success(function() {
$templateCache.removeAll();
$route.reload();
});
}
}
]);
※reconstruction()はsymfonyのバリデーションを通過させるためにオブジェクトの構造を構築し直す俺々メソッド。jQuery.ajax()とAngularの$http.post()でサ
ーバー側に送信される構造が違う所為でくっそハマった。
方法。
php app/console cache:clear --no-warmup --env=prod --no-debug
でいける。
シンプルにエンティティをjsonで出力する方法。
namespace Hoge\FugaBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Hoge\FugaBundle\Entity\Post;
use FOS\RestBundle\Controller\FOSRestController;
/**
* Post controller.
*/
class PostController extends FOSRestController
{
/**
* Lists all Post entities.
*/
public function indexAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('HogeFugaBundle:Post')->findAll();
$view = $this->view($entities, 200)->setFormat('json');
return $this->handleView($view);
}
}
色々ハマったけどこれだけでOK。
手順メモ。
RestBundleはJMSSerializerBundleを必要とするのでまずJMSの方をインストールする。
php composer.phar require jms/serializer-bundle Please provide a version constraint for the jms/serializer-bundle requirement: dev-master
※バージョンは「dev-master」を指定
次にFOSRestBundleをインストールする。
php composer.phar require friendsofsymfony/rest-bundle 1.3.*
あとはカーネルで両方とも読みこめば完了。
詳細は下記URLを参照されたし。
http://jmsyst.com/bundles/JMSSerializerBundle
https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Resources/doc/index.md
例えば引数に名前が必要なURIに名前無しでアクセスさせたい場合。
hello_world:
pattern: /hello/{name}
defaults: { _controller: HogeBundle:Fuga:hello }
requirements:
name: ".+"
requirementsでnameのパターンを上記の様に指定してやればOK。
userBundleを使っていたところ、ある時期を堺にどうやってもログイン後に「/_wdt/…」にリダイレクトされてしまうようになった。
が、この方法で解決。
security.ymlに下記を定義する。
firewalls:
secured_area:
form_login:
always_use_default_target_path: true
default_target_path: /loggedinpage
「loggedinpage」の部分にログイン後のリダイレクトページを設定すればOK。
やり方メモ。
services:
my_service_request:
class: Symfony\Component\HttpFoundation\Request
my_service_response:
class: Symfony\Component\HttpFoundation\Response
my.listener.before.filter:
class: Hoge\FugaBundle\EventListener\ControllerListener
arguments: ['@service_container', '@my_service_request']
tags:
- { name: kernel.event_listener, event: kernel.request, method: piyo }
的な感じ。