【问题标题】:Yii pattern match for URLURL 的 Yii 模式匹配
【发布时间】:2014-01-26 06:52:43
【问题描述】:

我需要在我的Yii 应用程序中处理一个对 SEO 友好的 URL。我在模式中的 URL 结构:“domain.com/url-string/uniqueid”。例如:

domain.com/properties-in-dubai/325fgd
domain.com/properties-in-USA/4577ds6
domain.com/properties-in-india/567dew
domain.com/apartments-in-anderi-mumbai/645fki

以上 URL 字符串和 ID 由我们填充。当用户访问此 URL 时:

  1. 首先需要验证 URL 模式。
  2. 第二次提取 I'd 和 URL 字符串并与我的数据存储匹配。
  3. 第三个当上面的 URL 和我存在于我们的数据存储中时,将引用的参数传递给现有的搜索页面。

请任何人帮我解决这个问题并让我睡觉。

【问题讨论】:

    标签: php yii url-rewriting custom-url


    【解决方案1】:

    首先,向urlManager 应用程序配置添加新规则。

    'rules' => array(
        '<slug>/<id:\d+>' => 'property/view'
        // ...
    )
    

    然后您可以在操作中检索 slug 和 ID:

    class PropertyController extends CController
    {
        public function actionView($slug, $id)
        {
            $id = (int) $id;
    
            // Check if $id, $slug exist; replace line below
            $exists = true;
    
            if($exists){
                // Redirect to elsewhere
                $this->redirect();
            }
        }
    }
    

    【讨论】:

    • 感谢您的回答。但是当我使用重定向时,搜索引擎友好的 URL 会发生变化。是否可以在重定向时保持相同的 url
    猜你喜欢
    • 1970-01-01
    • 2012-06-18
    • 2019-03-29
    • 2013-10-07
    • 2011-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多