在CakePHP中的可变预定路由 [英] Variable Prefixed Routing in CakePHP

查看:95
本文介绍了在CakePHP中的可变预定路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在CakePHP中创建一个应用程序,需要我在一个CakePHP安装中运行'多个'应用程序。像我有 n 控制器的行为相同的所有应用程序,但它们只有不同,当我调用数据库 - 反正,我需要创建一个行为类似这样的路由:

 / app1 / controller / action / a / b / c 



 / app2 / controller / action / a / b / c 

(其中 app1 app2 是可以更改为任何内容的字母数字字符串)



这将被路由到类似:

 controller / action / app1 / a / b / c 

(或app2的相同,等等)



路由路由可能只是 / controller / action / a / b / c ,但我需要有一种方法来访问 app1 app2 控制器中的URL部分(用于在控制器中进一步处理)。有没有办法在CakePHP中做到这一点?感谢。



稍微相关的问题:当上述操作完成后,是否有一种方法可以设置默认我试图访问 / controller / action / a / b / c 它会自动路由到相当于输入 / global / controller / action / a / b / c ?)



谢谢!



我想要的只是使用路由(或任何其他CakePHP'的方法,可以这样做)来处理像 / foobar / controller / action / the / rest / controller / action / the / rest 并将foobar传递给控制器​​。 c> app / Config / routes.php

c $ c> add:

  Router :: connect(
'/:app /:controller / *',
array(),
array('pass'=> array('app'))
);

这将传递 app 控制器中的动作的第一个参数。所以在你的控制器中你会做类似于:

  class FoosController扩展AppController {
public function view_something $ a,$ b,$ c){
// ...
}
}

$ b b

当您请求 / myApp1 / foos / view_something / 1/2/3 的值 $ app 将为'myApp1' $ a 的值为 1



要连接其他路线,之前,您可以添加以下内容:

  Router :: connect(
'/ pages /:action / *',
array >'global','controller'=>'pages'),
array('pass'=> array('app'))//使控制器中的第1个参数
) ;


I'm creating an app in CakePHP which requires me to run 'multiple' apps within one CakePHP installation. Something like I have n controllers that behave the same for all applications, but they only differ when I call the database - anyway, I need to create a route which behaves something like this:

/app1/controller/action/a/b/c

/app2/controller/action/a/b/c

(where app1 and app2 are alphanumeric strings that can change to anything)

That would be routed to something like:

/controller/action/app1/a/b/c

(or the same for app2, and so on)

The routed route could be just /controller/action/a/b/c too, but I need to have a way to access the app1 / app2 parts of the URL within the controller (for further processing within the controller). Is there a way to do this in CakePHP? Thanks.

Slightly related question: When the above is accomplished, is there a way to set a 'default' app-name (like when I attempt to access /controller/action/a/b/c it will automatically be routed to the equivalent of typing /global/controller/action/a/b/c?)

Thanks!

Effectively: What I want is just to use Routing (or any other CakePHP 'method' that can do this) to handle URLs like /foobar/controller/action/the/rest to /controller/action/the/rest and pass "foobar" to the controller, somehow. "Foobar" is any alphanumeric string.

解决方案

In app/Config/routes.php add:

Router::connect( 
    '/:app/:controller/:action/*', 
    array(), 
    array( 'pass' => array( 'app' ))
);

This will pass the value of app as the first argument to the action in your controller. So in your controller you would do something like:

class FoosController Extends AppController {
    public function view_something($app, $a, $b, $c) { 
        // ...
    }
}

When you request /myApp1/foos/view_something/1/2/3 the value of $app would be 'myApp1', the value of $a would be 1, etc.

To connect other routes, before the above, you can add something like:

Router::connect(
    '/pages/:action/*',
    array( 'app' => 'global', 'controller' => 'pages' ),
    array( 'pass' => array( 'app' )) // to make app 1st arg in controller
);

这篇关于在CakePHP中的可变预定路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆