参考
http://symdoc.kwalk.jp/doc/book/configuration
設定手順
- インストール
- wget https://symfony.com/installer
- mv installer symfony
- プロジェクト作成
- symfony new hoge [2.8とか]
- 設定
- 設定確認 web/config.php
- localhost以外からアクセスする場合はconfig.phpを一時的に書き換え
- 書き込み権限
- apacheまたは、apacheを含むグループに書込とSGIDを立てておくと良い
- サブディレクトリに配置する場合でも、htaccess等の編集は不要
- 設定確認 web/config.php
- apacheの設定
- Options -MultiViews が許可されていない等のエラーが出る場合
- AllowOverride に Options=All,Multiviews が必要。
- Options -MultiViews が許可されていない等のエラーが出る場合
HelloWorld
2系も3系もディレクトリ構造が少し違うがHelloWorld的なサンプルはあまり気にしなくて良い。
Controller
src/AppBundle/Controller/HelloController.php
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class HelloController extends Controller
{
/**
* @Route("/hello")
*/
public function indexAction(Request $request)
{
// replace this example code with whatever you need
return $this->render(
'hello/index.html.twig',
array('message' => "hello world!"));
}
}
View
app/Resources/views/hello/index.html.twig
サンプルページ
{{message}}
予備知識
キャッシュ
ルートやビューの表示の一部は最初の実行時に決定され、キャッシュに保存される。
そのため、ルートの変更や表示を変更した場合はキャッシュをクリアする必要がある。
デバッグモードの場合は、キャッシュされないが、パフォーマンスに影響する。
デバッグモードの切替
app.php
$kernel = new AppKernel('prod', false);
のAppKernelの引数で環境とデバッグモードを指定する。
- 第1引数
- prod、dev、testの環境
- 第2引数
- デバッグモードの指定。trueの場合デバッグモードになる。
各環境はロードする設定ファイルが異なる。
consoleのパス
- 2系 php app/console
- 3系 php bin/console
umask
groupとSGIDもちゃんと設定すれば
app.phpの最初かapacheの設定で umask を 002 にしておくと楽かも。
コマンド
ルート一覧
console router:debug
キャッシュクリア
console cache:clear --dev=prod --no-warmup
- --dev
- 環境を指定。
- --no-warmup
- キャッシュの初期データを作成しない
テスト用のサーバ起動、終了
console server:start console server:stop
ロガー
$logger = $this->get('logger');
$logger->info('I just got the logger');
$logger->err('An error occurred');
セッション
3系
use Symfony\Component\HttpFoundation\Session\Session;
$session = new Session();
$session->set('name', 'Drak');
$session->get('name');
2系
$session = $this->getRequest()->getSession();
$session->set('foo', 'bar');
$foo = $session->get('foo');
ルーティング
今のところ、アノテーションでルーティング。
データベース
SQL実行
$stmt = $this->getDoctrine()->getEntityManager()->getConnection()->prepare('select * from test where id = :id');
$stmt->execute(array("id" => 1));
$result = $stmt->fetchAll();
$stmt->closeCursor();
ORM
php app/console doctrine:mapping:convert annotation src --from-database
$em = $this->getDoctrine()->getManager();
$test = new Test();
$test->setId(1);
$test->setName("hoge");
$em->persist($test);
$em->flush();
[カテゴリ: プログラミング言語 > PHP]
[通知用URL]
Tweet
最終更新時間:2017年09月25日 20時03分23秒