Practical symfony 5日目

#### Practical symfony 5日目(http://www.symfony-project.org/jobeet/1_4/Doctrine/ja/05)
#### 内部 URI と 外部 URL のマッピングを確認する。
#### url_for() の中の書き方は日本語版を読んでも英語版を読んでも、どっちがいいのかよくわからん
#### とりあえず書き換える
> notepad .\apps\frontend\config\routing.yml
  # param: { module: default, action: index }
  param: { module: job, action: index }

> notepad .\apps\frontend\templates\layout.php
					<h1>
						<a href="<?php echo url_for('homepage') ?>">
							<img src="/images/logo.jpg" alt="Jobeet Job Board" />
						</a>
					</h1>

#### ブラウザで http://jobeet.localhost/frontend_dev.php/ にアクセスすれば表示できるものの、
#### http://jobeet.localhost/ とかは最初のままなんですけど……?
> symfony cache:clear

#### 解決。That was easy! ……と思いきや http://jobeet.localhost/job/show/id/1 とかが出ない
#### mod_rewrite 有効化しないと .\web\.htaccess の設定が効かないのね
#### httpd.conf の該当行をコメント解除して Apache を再起動
LoadModule rewrite_module modules/mod_rewrite.so

#### 解決。Was that easy?

#### あたらしいルートを作成
> notepad .\apps\frontend\config\routing.yml
job_show_user:
  url:   /job/:company/:location/:id/:position
  param: { module: job, action: show }

> notepad .\apps\frontend\modules\job\templates\indexSuccess.php
<a href="<?php echo url_for('job/show?id='.$job->getId().'&company='.$job->getCompany(). '&location='.$job->getLocation().'&position='.$job->getPosition()) ?>">

#### できた

#### ルーティングの所に正規表現での validation を記述
> notepad .\apps\frontend\config\routing.yml
job_show_user:
  url:   /job/:company/:location/:id/:position
  param: { module: job, action: show }
  requirements:
    id: \d+

#### 効いてるのかどうかわからなかったので色々追加
job_show_user:
  url:   /job/:company/:location/:id/:position
  param: { module: job, action: show }
  requirements:
    id: \d+
    position: \S+
    company: \S+
#### validation に失敗したときの振る舞いはどこで決めるんだろう……?

#### HTTP メソッドを限定
> notepad .\apps\frontend\config\routing.yml
job_show_user:
  url:   /job/:company/:location/:id/:position
  class: sfRequestRoute
  param: { module: job, action: show }
  requirements:
    id: \d+
    sf_method: [get]

#### sfDoctrineRoute クラスを使うように変更
> notepad .\apps\frontend\config\routing.yml
job_show_user:
  url:     /job/:company/:location/:id/:position
  class:   sfDoctrineRoute
  options: { model: JobeetJob, type: object }
  param:   { module: job, action: show }
  requirements:
    id: \d+
    sf_method: [get]

> notepad .\apps\frontend\modules\job\templates\indexSuccess.php
<a href="<?php echo url_for(array('sf_route' => 'job_show_user', 'sf_subject' => $job)) ?>">

#### 動いてるんだけど駆け足でわかりづらい。もう少しちゃんと把握したほうがいいかも。

#### Jobeet クラスを定義 ... BOM なし UTF-8 で保存
> notepad .\lib\Jobeet.class.php
<?php
// lib/Jobeet.class.php
class Jobeet
{
	static public function slugify($text)
	{
		// 文字ではないもしくは数値ではないものすべてを - に置き換える
		$text = preg_replace('/\W+/', '-', $text);
		
		// トリムして小文字に変換する
		$text = strtolower(trim($text, '-'));
		
		return $text;
	}
}
?>
#### 最後の ?> いらないのか……

#### スラグ化されたゲッターを定義
> notepad .\lib\model\doctrine\JobeetJob.class.php
	public function getCompanySlug()
	{
		return Jobeet::slugify($this->getCompany());
	}
	
	public function getPositionSlug()
	{
		return Jobeet::slugify($this->getPosition());
	}
	
	public function getLocationSlug()
	{
		return Jobeet::slugify($this->getLocation());
	}

#### スラグ化版を使うように修正(この辺り全然理解できてない)
> notepad .\apps\frontend\config\routing.yml
job_show_user:
  url:     /job/:company_slug/:location_slug/:id/:position_slug
  class:   sfDoctrineRoute
  options: { model: JobeetJob, type: object }
  param:   { module: job, action: show }
  requirements:
    id: \d+
    sf_method: [get]

#### ブラウザで http://jobeet.localhost/frontend_dev.php/job/sensio-labs/paris-france/1/web-developer にアクセス

#### getRoute() を使うように書き換え……意味わかってない。
> notepad .\apps\frontend\modules\job\actions\actions.class.php
$this->job = $this->getRoute()->getObject();

#### allow_empty オプションを true にすると getRoute() が例外を投げなくなる -> そしてエラーになる
> notepad .\apps\frontend\config\routing.yml
job_show_user:
  url:     /job/:company_slug/:location_slug/:id/:position_slug
  # url:     /job/:company/:location/:id/:position
  class:   sfDoctrineRoute
  options: { model: JobeetJob, type: object, allow_empty: true }
  param:   { module: job, action: show }
  requirements:
    id: \d+
    sf_method: [get]

#### link_to() で絶対パスを生成する部分、ドキュメントが間違ってる(英語版も間違ってる)。
#### API リファレンスの該当部分(http://www.symfony-project.org/api/1_4/UrlHelper#method_link_to)を
#### 参照すると以下の感じ
> notepad .\apps\frontend\modules\job\templates\indexSuccess.php
<?php echo link_to("permlink", 'job_show_user', $job, array('absolute' => true)) ?>

#### アクションから URL を生成する例は、リダイレクトがループしてるエラーで確認。なのですぐコメントアウト
> notepad .\apps\frontend\modules\job\templates\indexSuccess.php
$this->redirect($this->generateUrl('job_show_user', $this->job));

#### job アクションを定義
> notepad .\apps\frontend\config\routing.yml
job:
  class:   sfDoctrineRouteCollection
  options: { model: JobeetJob }

#### _form.php を確認
> notepad .\apps\frontend\modules\job\templates\_form.php

#### 設定されている route を確認
> symfony app:routes frontend
>> app       Current routes for application "frontend"
Name                   Method          Pattern
job           GET    /job.:sf_format
job_new       GET    /job/new.:sf_format
job_create    POST   /job.:sf_format
job_edit      GET    /job/:id/edit.:sf_format
job_update    PUT    /job/:id.:sf_format
job_delete    DELETE /job/:id.:sf_format
job_show      GET    /job/:id.:sf_format
job_show_user GET    /job/:company_slug/:location_slug/:id/:position_slug
homepage      ANY    /
default_index ANY    /:module
default       ANY    /:module/:action/*

> symfony app:routes frontend job_edit
>> app       Route "job_edit" for application "frontend"
Name         job_edit
Pattern      /job/:id/edit.:sf_format
Class        sfDoctrineRoute
Defaults     action: 'edit'
             module: 'job'
             sf_format: 'html'
Requirements id: '\\d+'
             sf_format: '[^/\\.]+'
             sf_method: array (0 => 'get',)
Options      cache: NULL
             context: array ()
             debug: true
             default_action: 'index'
             default_module: 'default'
             extra_parameters_as_query_string: true
             generate_shortest_url: true
             load_configuration: false
             logging: false
             lookup_cache_dedicated_keys: false
             method: NULL
             model: 'JobeetJob'
             segment_separators: array (0 => '/',1 => '.',)
             segment_separators_regex: '(?:/|\\.)'
             suffix: ''
             text_regex: '.+?'
             type: 'object'
             variable_content_regex: '[^/\\.]+'
             variable_prefix_regex: '(?:\\:)'
             variable_prefixes: array (0 => ':',)
             variable_regex: '[\\w\\d_]+'
Regex        #^/job/(?P<id>\d+)/edit(?:\.(?P<sf_format>[^/\.]+))?$#x
Tokens       separator  array (0 => '/',1 => NULL,)
             text       array (0 => 'job',1 => NULL,)
             separator  array (0 => '/',1 => NULL,)
             variable   array (0 => ':id',1 => 'id',)
             separator  array (0 => '/',1 => NULL,)
             text       array (0 => 'edit',1 => NULL,)
             separator  array (0 => '.',1 => NULL,)
             variable   array (0 => ':sf_format',1 => 'sf_format',)

#### default のルートを削除
> notepad .\apps\frontend\config\routing.yml
# generic rules
# please, remove them by adding more specific rules
# default_index:
#   url:   /:module
#   param: { action: index }
# 
# default:
#   url:   /:module/:action/*

> symfony app:routes frontend
>> app       Current routes for application "frontend"
Name                   Method          Pattern
job           GET    /job.:sf_format
job_new       GET    /job/new.:sf_format
job_create    POST   /job.:sf_format
job_edit      GET    /job/:id/edit.:sf_format
job_update    PUT    /job/:id.:sf_format
job_delete    DELETE /job/:id.:sf_format
job_show      GET    /job/:id.:sf_format
job_show_user GET    /job/:company_slug/:location_slug/:id/:position_slug
homepage      ANY    /

#### 5日目終了。ドキュメントの品質もアレげになりつつあり、理解もちょっと不安になりつつある。

5日目の謎な部分を確認している時にみつけたページ
Jobeetをやってみる 5日目