jobeet 5日目: ルーティング
apps/frontend/config/routing.ymlを編集した場合の動作を確認する
- apps/frontend/config/routing.ymlを編集
- lib/model/doctrine/JobeetJob.class.phpを編集
- lib/Jobeet.class.phpを追加
- apps/frontend/modules/job/templates/_form.phpを編集
- apps/frontend/config/routing.ymlの一部をコメントアウト
apps/frontend/config/routing.ymlを編集
# apps/frontend/config/routing.yml job: class: sfDoctrineRouteCollection options: { model: JobeetJob } 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] # default rules homepage: url: / param: { module: job, action: index } default_index: url: /:module param: { action: index } default: url: /:module/:action/*
lib/model/doctrine/JobeetJob.class.phpを編集
class JobeetJob extends BaseJobeetJob { public function __toString() { return sprintf('%s at %s (%s)', $this->getPosition(), $this->getCompany(), $this->getLocation()); } public function getCompanySlug() { return Jobeet::slugify($this->getCompany()); } public function getPositionSlug() { return Jobeet::slugify($this->getPosition()); } public function getLocationSlug() { return Jobeet::slugify($this->getLocation()); } }
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; } }
apps/frontend/modules/job/templates/_form.php
<form action="..." ...> <?php if (!$form->getObject()->isNew()): ?> <input type="hidden" name="sf_method" value="PUT" /> <?php endif; ?> <?php echo link_to( 'Delete', 'job/delete?id='.$form->getObject()->getId(), array('method' => 'delete', 'confirm' => 'Are you sure?') ) ?>
apps/frontend/config/routing.ymlの一部をコメントアウト
#default_index: # url: /:module # param: { action: index } # #default: # url: /:module/:action/*