php cakephp 创建重定向到视图的简单按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13491421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:34:41  来源:igfitidea点击:

cakephp create simple buttons that redirects to views

phpformscakephp

提问by user1511579

i'm starting working with cakephp and i'm having some problems in building the following: i want the main page of my application have 4 simple buttons, that redirects to different created views. How can i do that?

我开始使用 cakephp 并且我在构建以下内容时遇到了一些问题:我希望我的应用程序的主页有 4 个简单的按钮,这些按钮重定向到不同的创建视图。我怎样才能做到这一点?

I tried this:

我试过这个:

   <?php
echo $this->Form->create('Post');
echo $this->Form->button('Redirect1', array('type'=>'button'));
echo $this->Form->button('Redirect1', array('type'=>'button'));
echo $this->Form->button('Redirect1', array('type'=>'button'));
echo $this->Form->end();
?>

but i guess is no need for a submit form right? However i didn't found other way in my searches. Is not possible just to change page on button click?

但我想不需要提交表格吧?但是我在搜索中没有找到其他方式。不能仅在单击按钮时更改页面吗?

Thank you

谢谢

回答by Alvaro

Form buttons should not be used as simple links, anyway, you create the button and then adding some javascript or jQuery to make it work as you want as Justin said. (although i usually prefer to use an external .js file with the code)

表单按钮不应该用作简单的链接,无论如何,您创建按钮,然后添加一些 javascript 或 jQuery 以使其按您想要的方式工作,正如 Justin 所说。(虽然我通常更喜欢使用带有代码的外部 .js 文件)

Another option, would be adding an style for the link which could give it the appearance of a button, for example:

另一种选择是为链接添加样式,使其具有按钮的外观,例如:

echo $this->Html->link("Demo", array('controller' => 'yourcontroller','action'=> 'youraction', $possibleParameter), array( 'class' => 'button'))

Where button could be defined as:

其中按钮可以定义为:

a.button {
    color: #6e6e6e;
    font: bold 12px Helvetica, Arial, sans-serif;
    text-decoration: none;
    padding: 7px 12px;
    position: relative;
    display: inline-block;
    text-shadow: 0 1px 0 #fff;
    -webkit-transition: border-color .218s;
    -moz-transition: border .218s;
    -o-transition: border-color .218s;
    transition: border-color .218s;
    background: #f3f3f3;
    background: -webkit-gradient(linear,0% 40%,0% 70%,from(#F5F5F5),to(#F1F1F1));
    background: -moz-linear-gradient(linear,0% 40%,0% 70%,from(#F5F5F5),to(#F1F1F1));
    border: solid 1px #dcdcdc;
    border-radius: 2px;
    -webkit-border-radius: 2px;
    -moz-border-radius: 2px;
    margin-right: 20px;
    cursor:pointer;
}
a.button:hover{
    color: #333;
    border-color: #999;
    -moz-box-shadow: 0 2px 0 rgba(0, 0, 0, 0.2); 
-webkit-box-shadow:0 2px 5px rgba(0, 0, 0, 0.2);
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.15);
}
a.button:active {
    color: #000;
    border-color: #444;
}

回答by scx

I found that question while searching for similar problem. In my opinion easiest way to do that would be that:

我在搜索类似问题时发现了这个问题。在我看来,最简单的方法是:

$this->Html->link($this->Form->button('Button'), array('action' => 'viewSomethin',$id), array('escape'=>false,'title' => "Click to view somethin"));

Maybe that will be useful for someone.

也许这对某人有用。

回答by Nick Zinger

I think you just need links which you would later style with css(assuming you're using the Html helper):

我认为您只需要稍后使用 css 设置样式的链接(假设您使用的是 Html 助手):

<?php 
echo $this->Html->link('Click Here', '/redirect/url', array('class' => 'button')); 
?>

Read more here: http://book.cakephp.org/1.3/en/view/1442/link

在此处阅读更多信息:http: //book.cakephp.org/1.3/en/view/1442/link

回答by Nipun Tyagi

you can use

您可以使用

<?php echo $this->Html->link('ADD New One', '/controllername/functionname')?>

回答by CodeWhisperer

You can add a Html rule attribute to your link. This rule will set the link as a button.

您可以向链接添加 Html 规则属性。此规则将链接设置为按钮。

echo $this->Html->link('Blogs', 
    array('action' => 'add'), 
    array(
        'bootstrap-type' => 'primary',
        'class' => 'btn btn-lg btn-primary btn-block',
        // transform link to a button
        'rule' => 'button'
    )
);

回答by Yuchao Zhou

For CakePHP 2.X

对于 CakePHP 2.X

 <button id="create_task_groups" onclick="window.location.href='<?php echo $this->Html>url(array('plugin'=>'task_calendar', 'controller'=>'task_groups', 'action'=>'add'))?>'"> Create Task Group</button>

回答by Justin John

Something like below will work for you

像下面这样的东西对你有用

$this->Form->button('Home', 
      array('onclick' => "location.href='".$this->Html->url($url)."'")); 

回答by Florin Lungu

I think the best solution is using postButton such as:

我认为最好的解决方案是使用 postButton,例如:

echo $this->Form->postButton('Text Button', array('controller'=>'controllername','action'=>'action_to_do'));

You have to change 'controllername' and 'action_to_do' with what you need

您必须根据需要更改“controllername”和“action_to_do”

回答by TheFRedFox

I also was just looking about this "problem". But I wasn't happy with the buttons of the form and I just want to get buttons like in the action class: enter image description here

我也只是在寻找这个“问题”。但是我对表单的按钮不满意,我只想获得类似操作类中的按钮:在此处输入图片说明

So I found 2 solutions for me:

所以我为我找到了两个解决方案:

Solution 1: Wrap the button in a span with the css class "action"

解决方案 1:使用 css 类“action”将按钮包裹在一个 span 中

<span class="action">
    <?php
    echo $this->Html->link("Send Again", "url");
    ?>
</span>

The css class actionis already meant in the cake.generic stylesheet. So it will be directly a nice button.

css 类action已经包含在 cake.generic 样式表中。所以它将直接成为一个不错的按钮。

Solution 2: Copy the css styles for .actions a

解决方案2:复制css样式 .actions a

This solution is more an addition to Alvaro's one. I can't comment yet, so I have to do an extra answer for that.

此解决方案更像是对Alvaro解决方案的补充。我还不能发表评论,所以我必须为此做一个额外的回答。

So, I didn't use Alvaro's css as they are for the form buttons and slightly different to the original ones. So I just searched for the styles for .action ain the original cake.generic.cssand copied them in my own css file and changed the selector to a.button.

所以,我没有使用 Alvaro 的 css,因为它们用于表单按钮并且与原始按钮略有不同。所以我只是.action a在原始cake.generic.css文件中搜索样式并将它们复制到我自己的 css 文件中并将选择器更改为a.button.

Then now you can use this to generate the button without any wrapper:

然后现在您可以使用它来生成没有任何包装器的按钮:

<?php echo $this->Html->link("Send Again, "url", ["class" => "button"]) ?>

The css for CakePHPversion 2.6.2:

CakePHP 2.6.2版的 css :

a.button {
  font-weight: normal;
  padding: 4px 8px;
  background: #dcdcdc;
  background-image: -webkit-gradient(linear, left top, left bottom, from(#fefefe), to(#dcdcdc));
  background-image: -webkit-linear-gradient(top, #fefefe, #dcdcdc);
  background-image: -moz-linear-gradient(top, #fefefe, #dcdcdc);
  background-image: -ms-linear-gradient(top, #fefefe, #dcdcdc);
  background-image: -o-linear-gradient(top, #fefefe, #dcdcdc);
  background-image: linear-gradient(top, #fefefe, #dcdcdc);
  color: #333;
  border: 1px solid #bbb;
  -webkit-border-radius: 4px;
  -moz-border-radius: 4px;
  border-radius: 4px;
  text-decoration: none;
  text-shadow: #fff 0px 1px 0px;
  min-width: 0;
  -moz-box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.3), 0px 1px 1px rgba(0, 0, 0, 0.2);
  -webkit-box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.3), 0px 1px 1px rgba(0, 0, 0, 0.2);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.3), 0px 1px 1px rgba(0, 0, 0, 0.2);
  -webkit-user-select: none;
  user-select: none;
}

a.button:hover {
  background: #ededed;
  border-color: #acacac;
  text-decoration: none;
}

a.button:active {
  background: #eee;
  background-image: -webkit-gradient(linear, left top, left bottom, from(#dfdfdf), to(#eee));
  background-image: -webkit-linear-gradient(top, #dfdfdf, #eee);
  background-image: -moz-linear-gradient(top, #dfdfdf, #eee);
  background-image: -ms-linear-gradient(top, #dfdfdf, #eee);
  background-image: -o-linear-gradient(top, #dfdfdf, #eee);
  background-image: linear-gradient(top, #dfdfdf, #eee);
  text-shadow: #eee 0px 1px 0px;
  -moz-box-shadow: inset 0 1px 4px rgba(0, 0, 0, 0.3);
  -webkit-box-shadow: inset 0 1px 4px rgba(0, 0, 0, 0.3);
  box-shadow: inset 0 1px 4px rgba(0, 0, 0, 0.3);
  border-color: #aaa;
  text-decoration: none;
}

Hope it will help guys three years after the question was asked. :D

希望它会在提出问题三年后对人们有所帮助。:D