简体   繁体   中英

SEO Friendly URLs with yii

I'm using php and Yii framework to create an web application but I`m stucked with some dynamic friendly URLs. I have the following scenario: in my view I have a form with a dropdown (where the user can select the SEASON) and two another hidden fields (eventID and playerName). When the user selects a season or click on the right panels (event or player) I submitted the form to the server with the selected values. Now what I want is to create the urls something like this:

www.domain.com/football/statistics/season-name/(eventID)?/(player-name)?/ -> where (*)? 0 or 1 time eg

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

Here is my view ( http://postimg.org/image/4r0xvgvw7/ ) and the view.php is:

<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>

I tried in main.cfg to set a rule something like this:

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

but no success.

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

I recommend you to write your own urlrule class.

Except of regExp routing it'will help to determine if player name exists in DB or not. In addition, it looks very clear when complex url routing is isolated in separate logical unit (class). You can learn more here http://yiiframework.ru/doc/guide/en/topics.url in section . 部分中了解更多信息。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM