重定向 CI 问题
我对 CodeIgniter 还算陌生,而且仍在学习(很多)。
所以我有一个视图,当我提交表单时,我通过动态浏览到正确的 URL 来“调用”控制器,例如 site/delete
class Site extends Controller {
function index(){$this->load->view('...')}
function delete() {
$this->site_model->delete_row();
$this->index();
}
}
现在,当该操作完成(删除行)时,我正在调用 $this->index( );重定向到我的初始页面(这很好),但我的网址仍然是: site/delete 。我希望我的 URL 为 ../site/index (或没有 /index)
任何帮助将不胜感激:-)。
I'm kind of new with CodeIgniter and I'm still learning (a lot).
So I have a view and when I submit a form I 'call' the controller by surfing to the right URL dynamically e.g. site/delete
class Site extends Controller {
function index(){$this->load->view('...')}
function delete() {
$this->site_model->delete_row();
$this->index();
}
}
Now when that action is done (deleted the row) I'm calling $this->index(); to redirect to my initial page (which is good) but my url stays: site/delete . I want my URL to be ../site/index (or without the /index)
Any help would be appreciated :-) .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
到目前为止,我找到了解决此问题的方法:
而不是:
我正在使用:
有人知道这是一个很好的做法吗?
So far I found something to solve this:
instead of:
I'm using:
Does anyone know this is a good practice?
重定向是您应该使用的。
在用户指南中:
http://codeigniter.com/user_guide/helpers/url_helper.html
他们使用它检查用户是否登录后。根据用户是否登录,他们重定向到不同的位置。
另请注意,重定向后的任何代码都不会运行。完成所需的所有操作后,请确保并重定向。
Redirect is what you should use.
In the user guide:
http://codeigniter.com/user_guide/helpers/url_helper.html
they use it after checking if a user is logged in. Depending on if they are or not, they redirect to a different place.
Also, note that any code after the redirect won't run. Make sure and redirect after you've done everything you need to.
我的首选方法是使用相同的方法处理类似的操作,用户随后会看到这些操作。
如果您之后以用户身份访问 /site/delete 会怎样?它将必须检测并抛出错误(显示消息)或重定向到适当的页面。 /site/delete 没有任何意义。
例如,如果用户在删除后通常会看到概述,那么我的表单将发布到 /site/index;在执行正常工作之前,使用
index
快速检查条件并在同一控制器中调用_delete()
。这样,如果用户刷新页面或按“后退”,事情看起来应该是一致的。
另一个例子是 /settings/edit 会向自身发布 - 这意味着它可以对帖子进行操作并显示任何输出(例如验证错误)。这意味着我的网站上没有
/settings/do_edit
位置,也意味着用户可以安全地返回/settings/edit
,并查看用于编辑其内容的表单设置。我认为这是对一个可能客观的问题的主观看法,我会鼓励大家就我的观点提供反馈,但这是我避免您所问问题的方式。
My preferred method is to have actions like that handled by the same method that will be seen by the user afterwards.
What if you go to /site/delete afterwards, as a user? It will either have to detect and throw a error (show a message) or redirect to an appropriate page. /site/delete has no meaning.
For example, if a user would normally see an overview after deleting, then my form will be posted to /site/index; with
index
quickly checking for the condition and calling_delete()
in the same controller, before doing its normal work.That way, if the user refreshes the page, or presses 'back', things should look consistent to them.
Another example would be that /settings/edit would post to itself - this means that it can act on the post and show any output (e.g. validation errors). It means there's no
/settings/do_edit
location on my site, and also means that the user can go back to/settings/edit
safely, and see a form for editing their settings.I suppose this is a subjective take on a perhaps objective question, and I would encourage feedback on my view, but it's my way of avoiding the problem you have asked about.
函数中的函数调用只是执行该函数内的功能。
并且网址从未改变。
要更改您应该使用的网址。
但你应该在构造函数中加载 url 帮助器。
Call of function in a function simply execute the functionality within that function.
And url never changed.
for changing the url you should use.
but you should load url helper in constructor.