简体   繁体   English

如何在CakePHP中编写指向特定控制器和动作的HREF链接?

[英]How do you write an HREF link to a specific controller and action in CakePHP?

I have a small application. 我有一个小应用程序。 I have written some of the code in index.ctp file and this file is working fine. 我已经在index.ctp文件中编写了一些代码,该文件工作正常。 Now I have added another function called add to my Controller file and also created a seperate file called add.ctp in Views folder. 现在,我在Controller文件中添加了另一个名为add函数,并且在Views文件夹中创建了一个名为add.ctp的单独文件。

My doubt is how to navigate to add.ctp file if I click a link. 我的疑问是,如果单击链接,如何导航到add.ctp文件。 I want to create a menu in my main file and write a link so that when I click on that link the page must navigate to add() function. 我想在主文件中创建一个菜单并编写一个链接,以便当我单击该链接时,页面必须导航至add()函数。

My problem is: 我的问题是:

  1. How to write href in cake php? 如何在Cake PHP中编写href?
  2. What link do I provide? 我提供什么链接? Controller or Views? 控制器还是视图?

The manual would be a good place to start your CakePHP journey. 手册将是您开始CakePHP旅程的好地方。 Then you'd quickly notice it's very easy to link to a controller action from your views with: 然后您会很快注意到,使用以下方法从您的视图链接到控制器动作非常容易:

echo $this->Html->link('Link text', array(
    'controller' => 'controller_name',
    'action' => 'add'
));

You can create hyperlink in view like this: 您可以在视图中创建超链接,如下所示:

echo $this->link('text',
    array('controller'=>'controllerName','action'=>'actionName'));

you are able to create URLs just from controllerName and it's action: 您可以仅从controllerName创建URL,其作用是:

echo $this->Html->url(
    array('controller' => 'controllerName','action' =>'actionName'),true);?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM