Laravel-从视图中调用Redirect :: to() [英] Laravel - Calling Redirect::to() from within view

查看:454
本文介绍了Laravel-从视图中调用Redirect :: to()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究基于Laravel 4框架构建的cms.我正在尝试构建一个类似于Pyro CMS的插件系统,在该系统中,可以使用Blade模板系统将模块视图包含在页面视图中.

I'm currently working on a cms which is built on the Laravel 4 framework. I'm trying to build a plugin system, similar to Pyro CMS, where module views can be included in a page view using the Blade template system.

我正在构建一个联系表单插件,如果提交成功,它将把用户重定向到给定的URL,或者简单地重定向回现有页面.

I'm building a contact form plugin that if submitted successfully will redirect the user to a given url, or simply redirect back to the existing page.

我的联系表单类的代码是:

The code for my contact form class is:

class Contact {

public static function form($params)
{
    //get params and execute relevant logic here

    $redirect = isset($params['redirect']) ? $params['redirect'] : Request::url();

    $data = Input::all();

    if($data)
    {
        // Run validation and send message here
        return Redirect::to($redirect)
    }

    return View::make('contact_form_view');
}

}

有一个页面控制器将根据使用的路线显示适当的页面视图,其思想是用户可以将联系人表单拖放到任何页面模板中,并可以通过从模板视图中调用表单函数轻松地对其进行自定义如下图所示

There is a page controller that will display the appropriate page view depending on the route used and the idea is that a user can drop a contact form into any page template and easily customise it by calling the form function from within the template view as shown below

<html>
    <head>
    </head>
    <body>

        {{ Contact::form(array(

            'to' => 'myemail@mydomain.com',
            'view'  => 'contact_form_1',

        )) }}

    </body>
</html>

除了重定向之外,所有这些都可以正常工作.成功提交表单并发送消息后,页面将刷新并显示以下消息代替联系表单

This all works fine apart from the redirect. When the form is successfully submitted and the message sent the page refreshes and displays the following message in place of the contact form

HTTP/1.0 302 Found Cache-Control: no-cache Date: Tue, 17 Sep 2013 09:14:16 GMT Location: http://localhost:8888/my_initial_route Redirecting to http://localhost:8888/my_new_route.

大约2秒钟后,重定向发生,浏览器被重定向到用户在$ params数组中提供的任何路由,如果没有提供重定向,则重定向到当前页面,如上例所示.

About 2 seconds later the redirect then takes place and the browser is redirected to whatever route the user provides in the $params array, or to the current page if no redirect is provided, as in the example above.

关于为什么要这样做的任何建议以及任何解决方案都将是很好的.

Any suggestions as to why it is doing this, as well as any solutions, would be great.

谢谢!

这里是我为什么采用上述方法的简要概述,这可能有助于理解问题:

Here is a really brief overview of why I have taken the above approach, which might help with understanding the problem:

该项目的要求是,最终将使用cms的开发人员可以通过cms控制面板中的添加页面"按钮来创建无限量的页面.我们的想法是,我们不会为要创建的每个新页面编写新的页面控制器.考虑到这一点,我有以下路线:

A requirement of the project is that the developers that will eventually use the cms can create a indefinite amount of pages via the cms control panel by clicking an "Add page" button. The idea is that we aren't writing new page controllers for every new page we want to create. With that I mind I have the following route:

Route::any('{page_slug}/page', array('as' => 'frontend.page', 'uses' => 'FrontendPagesController@display_page'))->where('page_slug',  '[-A-Za-z0-9_-]+');

大大简化了display_page功能的是:

public function display_page($slug)
{
    $page = PagesModel::getPageBySlug($slug);
    return View::make($page['view_name']);
}

用户可以通过单击按钮来创建页面,为其命名并添加一个标签,然后应用程序可以显示该页面以及在创建页面时定义的相应视图,而无需添加任何新的路线,控制器等.

With this users can create a page with the click of a button, give it a name and a slug, and the app can then display that page and the corresponding view -- defined when creating the page -- without having to add any new routes, controllers etc.

问题出现在用户希望在页面中包含联系表单的情况下.我们如何知道他们是否将添加表单,他们将使用哪些字段以及需要进行哪些验证?我需要一种能够将完全自定义的联系表单添加到任何页面中的方法,而开发人员无需触摸cms代码.我对此的解决方案是上面给出的方法:开发人员将标签放入视图中,然后将该函数传递给一些参数,这些参数可以自定义表单功能.

The problem arrises in a situation where a user wants to include a contact form with a page. How do we know if they will add a form or not, what fields they will use and what validation it will require? I need a way of being able to add a completely custom contact form into any page without the developer having to touch touch the cms code. My solution to this was the approach given above: the developer drops the tag into the view and passes the function some params, which customise the forms functionality.

Contact::form()函数有点像表单的控制器.它获取并返回包含来自html的视图,处理提交并根据结果返回成功/错误消息.

The Contact::form() function is kind of like a controller for the form. It gets and returns the view that contains the from html, processes the submission and returns success/errors messages depending on the outcome.

现在,我不必使用上面的方法,但是我需要一种将联系人表单控制器与页面控制器分离的方法,并且开发人员可以在不触摸cms后端代码的情况下将表单添加到任何页面.

Now I don't necessarily need to use the approach above but I need a way of separating the contact form controller from the page controller and for developers to be able to add a form to any page without touching the cms backend code.

任何有关此的想法都很棒.

Any ideas on this would be great.

推荐答案

您应该从控制器中调用redirect,而不是从视图中调用它.之所以发生这种情况,是因为您已经在视图中并且可以看到它了,所以如果您以某种方式可以使其隐藏(主体),那么在重定向发生之前它将不可见.

You should call the redirect from a controller instead of doing it from the view. This is happening because, you are already in the view and it's visible, so if you somehow could make it (the body) hidden then it won't be visible before redirect happens.

但是,您做错了,anti-pattern,IMO.使用控制器做出决定,视图应仅用于演示.这是MVC框架中非常基本的规则.

But, you are doing it wrong and anti-pattern, IMO. Use the controller to make decision, view should be used only for presentation. This is the very basic rule in an MVC framework.

这篇关于Laravel-从视图中调用Redirect :: to()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆