【问题标题】:Kohana 3, Detect dynamic uriKohana 3,检测动态uri
【发布时间】:2013-06-25 12:06:38
【问题描述】:

我正在做一个迷你网店系统,为了练习,你可以动态创建多个网店。

我目前有这个:

Route::set('dynamic_routes', function($uri)
{
    $webshop = DB::select()->from('webshops')
        ->where('uri', '=', $uri)
        ->execute()
        ->get('id', 0);

    // Check if there is a match
    if ($webshop > 0)
    {
        define('WEBSHOP_ID', $webshop);

        return array(
            'controller' => 'shop',
            'action' => 'index',
            'directory' => 'shop'
        );
    }
}
);

这将处理,因此我可以通过在数据库中查找 URI 来获得动态路由。

如果有匹配的网上商店,它会将您带到网上商店的索引。 - 工作正常。

现在这仅在您登陆网上商店的根 uri 时有效,例如“/myWebshop”。

对于所有网店,我有两个控制器,一个称为“shop”,另一个称为“customer”,我希望 /myWebshop/shop/action 和 /myWebshop/customer/action 可以访问它们

我的问题是“myWebshop”是动态的,“shop”控制器或“customer”控制器中的操作函数方法也是如此。

我如何编写两条动态路由?

这是我走了多远:

if(strpos($uri, '/'))
{
    // If we have a /, and [1] isn't empty, we know that the user looks for subpage?
    $expl = explode('/', $uri);

    if(!empty($uri[1]))
    {
        // Set the uri to the first part, in order to find the webshop?
        $uri = $uri[0];
    }


$webshop = DB::select()->from('webshops')
    ->where('uri', '=', $uri)
    ->execute()
    ->get('id', 0);

// Check if there is a match
if ($webshop > 0)
{
    define('WEBSHOP_ID', $webshop);

    return array(
        'controller' => 'shop',
        'action' => 'index',
        'directory' => 'shop'
    );
}
}

我不知道这之后该怎么办,我怎么知道创建动态路由并直接开始指向用户?

【问题讨论】:

    标签: php routes kohana kohana-3


    【解决方案1】:

    $uri 一样,从 URL 中取出控制器和操作部分:

    return array(
            'controller' => !empty($uri[1]) ? $uri[1] : 'some_default_controller',
            'action' => !empty($uri[2]) ? $uri[2] : 'some_default_action',
            'directory' => 'shop' // OR $uri[x] if it's also supposed to be dynamic
        );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-28
      • 2011-01-26
      • 2011-01-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多