在 CakePHP 的路由器文件中获取控制器名称和 SQL 查询?
是否可以在CakePHP的路由器文件中执行sql查询?查询必须选择有关控制器名称的数据。
有可能实现吗?
更新:我需要的是使用关于网址的不同路线。 url和route之间的关系将在数据库中定义。
tnx 广告!
is it possible to execute sql query in CakePHP's router file? query have to select data, regarding controller name.
it is possible to achieve?
update: what i need is to use different route regarding url. relation between url and route will be defined in database.
tnx in adv!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,您可以从路由器文件访问数据库并根据需要动态设置路由器。
为此,您必须在路由器文件中编写以下代码:
这里的 ModelName 是您要从中提取数据的模型的名称。
现在你有了模型的对象,现在你可以通过调用模型函数来获取数据并定义路由器。对于我的情况,我使用了这段代码,
因此,您可以根据您的要求以您的方式定义路由器。如果您仍然遇到问题,请与我联系..
谢谢
贝赫斯蒂
Yes you can access database from router file and set dynamically the router as you wish.
To do that you have to write this code in router file:
Here ModelName is the name of your model from which you want to pull the data.
So now you have the model's object, now you can get the data by calling model function and define the router. For my case I used this code
So, you can define your router in your way as your requirement. If you are still in problem, contact me..
Thanks
Behestee
在控制器中重定向是可能的,但它会产生开销,并且控制器背后的想法不是路由。否则路由类不存在。
实际上它是 Router 类的扩展,因此具有继承 router 类的连接功能的新类是最好的解决方案。
可以通过在数据库中查找 url 来完成路由。只需制作一个包含 url、控制器、操作和参数的表即可。 >getUrl()); ),然后使用 Router::connect( "results from the database" ) 连接
查找当前 url ( Router::normalize($ dispatcher-
沃特·德波尔.
Redirecting in a controller is possible, but it creates overhead, and the idea behind a controller is not routing. Otherwise the routing class wasn't there.
Actually it is an extension of the Router class, so a new class with your connect function that inherits the router class is the best solution.
It is possible to do your routing by lookup the urls in the database. Just make a table with url, controller, action and param. Lookup the current url ( Router::normalize($dispatcher->getUrl()); ) and then connect with Router::connect( "results from the database" )
Regards,
Wouter de Boer.