1

我正在使用 php 和 Yii 框架来创建一个 Web 应用程序,但我被一些动态友好的 URL 所困扰。我有以下场景:在我看来,我有一个带有下拉列表(用户可以在其中选择季节)和另外两个隐藏字段(eventID 和 playerName)的表单。当用户选择一个赛季或单击右侧面板(事件或球员)时,我将带有所选值的表单提交到服务器。现在我想要的是创建这样的网址:

www.domain.com/football/statistics/season-name/(eventID)?/(player-name)?/ -> where (*)? 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/',

但没有成功。

4

2 回答 2

1

您可以在http://yiiframework.ru/doc/guide/en/topics.url在使用自定义 URL 规则类部分了解更多信息。

于 2013-12-27T10:54:15.523 回答
0

我建议您编写自己的 urlrule 类。

除了 regExp 路由,它将有助于确定玩家姓名是否存在于 DB 中。另外,复杂的 url 路由在单独的逻辑单元(类)中隔离时看起来很清楚。您可以在http://yiiframework.ru/doc/guide/en/topics.url使用自定义 URL 规则类部分了解更多信息。

于 2013-08-05T01:49:49.123 回答