2013-07-27 79 views
1

我使用php和Yii框架來創建一個web應用程序,但我用一些動態友好的URL來填充。 我有以下情況:在我看來我有一個下拉(用戶可以選擇季節)和另外兩個隱藏的字段(eventID和playerName)的窗體。當用戶選擇一個季節 或點擊右側面板(事件或玩家)時,我使用所選值將表單提交給服務器。 現在我想要創建這樣的網址:用yii編寫的友好URLs

www.domain.com/football/statistics/season-name/(eventID)?/(player-name)?/ - >其中(*) ? 0或1次 例如

www.domain.com/football/stats/season-33/ 
www.domain.com/football/stats/season-33/91 
www.domain.com/football/stats/season-33/arno-celestini 
www.domain.com/football/stats/season-33/91/arno-celestini 

這是我的看法(http://postimg.org/image/4r0xvgvw7/)和view.php是:

<div id='main'> 
<div style="margin-left:50px;margin-top: 60px;width: 350px; border: 1px solid black; float:left;"> 
<?php $this->widget('zii.widgets.CBreadcrumbs', array(
    'homeLink' => CHtml::link('Home', Yii::app()->homeUrl), 
    'links'=> $this->breadcrumbs));?><p>Season: <?php echo $selectedSeason;?> <br/>player: <?php echo $selectedPlayer?> <br/>Id event: <?php echo $selectedEventId; ?></p> 
    <?php echo CHtml::beginForm('', 'get', array('id' => 'filters_form', 'action' => $formURL)); 
     echo CHtml::dropDownList(
      'season', 
      $selectedSeason, 
      CHtml::listData($seasons, 'season_id', 'name'), 
      array(
       'prompt' => 'Select a season', 
       'onchange' =>"js:$('#filters_form').submit()" 
      ) 
     );?> 
    <input type="hidden" name="eventId" id="event_field" value="<?php echo $selectedEventId;?>" /> 
    <input type="hidden" name="playerName" id="player_field" value="<?php echo $selectedPlayer;?>" /> 
    <?php echo CHtml::endForm(); ?> 
    <br/> 
    <?php foreach ($matches as $match) { ?> 
     <div class="match"> 
      <p><a href="<?php $this->createUrl('statistics/view', array('id' => $match->id, 'slug' => $match->match_date)); ?>"> 
      <?php echo $match->homeTeam->Name." <b>$match->home_goals - $match->away_goals</b> ".$match->awayTeam->Name; ?></a></p> 
      <p><?php $match->match_date;?></p> 
      <p><?php $match->match_type;?></p> 
      <p><?php $match->home_goals;?></p> 
      <p><?php $match->away_goals;?></p> 
      <hr/> 
     </div> 
    <?php } ?> 

</div> 
<div id="vertical_filters" style="float:left;margin-top:60px;"> 
    <div class="filter_left" style="margin-left:20px;"> 
     <ul> 
     <?php foreach ($events as $event) { ?> 
       <li class="list-item"> 
        <a class="event <?php if($selectedEventId == $event['id']) echo "selected";?>" href="<?php echo $event['id']; ?>"><?php echo $event['name']; ?></a> 
       </li> 
     <?php } ?> 
     </ul> 
    </div> 
    <br/> 
    <div class="filter_left" style="margin-left:20px;"> 
     <ul> 
     <?php foreach ($players as $player) { ?> 
       <li class="list-item"> 
        <a class="player <?php if ($selectedPlayer == $player['id']) echo "selected"; ?>" href="<?php echo $player['id']; ?>"><?php echo $player['name'] . ' ' . $player['surname']; ?></a> 
       </li> 
     <?php } ?>   
     </ul>   
    </div> 
</div> 
<script> 
    $(function() { 
     console.log($('#filters_form').attr('action')); 
     $('.event').click(function(e) { 
      e.preventDefault(); 
      value = $(this).attr('href'); 
      $('#event_field').val(value);     
      $('#filters_form').submit(); 
     }); 

     $('.player').click(function(e) { 
      e.preventDefault(); 
      value = ($(this).text() != 'All players')?$(this).text():''; 
      $('#player_field').val(value); 
      $('#filters_form').submit(); 
     }); 
    }); 
</script> 

我試圖在main.cfg設置的規則是這樣的:

'stats(/<season:\w+>)?(/<eventId:\d+>)?(/<playerName:\w+>)?' => 'statistics/index/', 

但沒有成功。

回答

0

我建議你編寫自己的urlrule類。

除regExp路由以外,它將幫助確定玩家名稱是否存在於數據庫中。另外,當複雜的URL路由在單獨的邏輯單元(類)中被隔離時,它看起來非常清楚。您可以在這裏瞭解更多http://yiiframework.ru/doc/guide/en/topics.url的部分使用自定義URL規則類