0

我正在制作一个迷你网店系统,以供练习,因此您可以动态创建多个网店。

我目前有这个:

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'
    );
}
}

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

4

1 回答 1

0

以与您相同的方式从 URL 中取出控制器和操作部分$uri

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
    );
于 2013-06-25T13:09:54.243 回答