从控制器更改布局内容

发布于 2024-10-08 09:15:57 字数 154 浏览 0 评论 0原文

我用的是Yii。我想在布局上有一个动态链接。此动态链接将由控制器修改。假设动态链接使用控制器给出的用户 ID 来执行任务。

我正在考虑使用 jQuery 脚本来获取控制器返回的用户 id,然后使用该用户 id 修改保存动态链接的 div。

您对这项技术有何看法?

I am using Yii. I want to have a dynamic link on a layout. This dynamic link will be modified by controllers. Let's say that dynamic link uses a user's id given by controllers to perform a task.

I am thinking to use jQuery script to get user id returned by controllers then use the user id to modify a div that holds the dynamic link.

What do you think about this technique?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红墙和绿瓦 2024-10-15 09:15:57

您似乎想在页面呈现后使用客户端 JavaScript 动态更改链接。但在服务器大小的 PHP 渲染过程中,第一次动态渲染不同的链接更有意义。毕竟,控制器生成视图!我将在页面请求期间从控制器获取用户 ID,将 ID 传递到视图,然后在初始页面加载时在视图中动态构建链接。

如果您要修改布局(而不是视图)中的链接,那么最好的办法是在控制器中创建一个变量,并使用视图设置该变量。看看 Yii 如何使用 $layout、$menu 和 $breadcrumbs 变量来做到这一点。

假设用户已登录并且您想要他们的 ID,您也可以从 Yii::app() 对象获取 ID,如下所示:

<?php echo CHtml::link('Edit user',array('user/edit','userId'=>Yii::app()->user->id)); ?>

但此时,您可以只请求用户的ID在控制器中,不需要建立这样的链接。

假设您想要一个与登录用户不同的用户 ID,请将该 ID ($userId) 从控制器传递到视图中,然后执行此操作(如 Moyersy 所说):

<?php echo CHtml::link('Edit user',array('user/edit','userId'=>$userId)); ?>

这将构建以下内容链接(其中 $userId = 99999999):

<a href="/user/edit?userId=99999999">Edit user</a>

因此,当单击链接时,在 actionEdit() 中,您现在可以通过 GET 变量 $_GET['userId'] 访问用户的 ID。

现在,如果您想要做的是更改已创建的链接,那么您需要使用 jQuery。但您需要更详细地解释为什么要这样做以及触发链接更改的原因(下拉菜单?)。

It seems like you want to dynamically change a link AFTER the page is rendered, with client-side JavaScript. But it makes more sense to dynamically render a different link the first time, during the server-size PHP rendering process. The controller generates the view, after all! I would get the user ID from the controller during the page request, pass the ID in to the view, and then build the link in the view dynamically on the initial page load.

If you are modifying a link in a layout (not a view), then the best thing to do is create a variable in the Controller, and set that variable with the view. Look at how Yii uses the $layout, $menu and $breadcrumbs variables to do this.

Assuming that the user is logged in and you want their ID, you can get the ID from the Yii::app() object as well, like so:

<?php echo CHtml::link('Edit user',array('user/edit','userId'=>Yii::app()->user->id)); ?>

But at that point, you can just request the user's ID in the controller, and don't need to build a link like this.

Assuming that you want a different user ID than the logged in user, pass that ID ($userId) from the controller into the view, and just do this (as Moyersy said):

<?php echo CHtml::link('Edit user',array('user/edit','userId'=>$userId)); ?>

This will build the following link (where $userId = 99999999):

<a href="/user/edit?userId=99999999">Edit user</a>

So when the linked is clicked, in the actionEdit() you now have access to the user's ID via the GET variable $_GET['userId'].

NOW, if what you want to do is change an already created link, then you would need to use jQuery. But you will need to explain in more detail why you are doing this and what is triggering the link change (a dropdown menu?).

予囚 2024-10-15 09:15:57

抱歉,我无法理解您想做什么。具体来说,我不明白动态链接是什么。

编辑:

<? echo CHtml::link('Edit user',array('user/edit','userId'=>$userId)); ?>

I'm sorry, I can't understand what you are trying to do. Specifically I don't understand what a dynamic link is.

Edit:

<? echo CHtml::link('Edit user',array('user/edit','userId'=>$userId)); ?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文