【问题标题】:Why redirect show blank page in model laravel?为什么在模型 laravel 中重定向显示空白页?
【发布时间】:2015-03-12 06:08:27
【问题描述】:

想问一下为什么下面的代码可以正常工作,重定向正常,数据插入成功:

类别控制器:

public function store()
     {
         $data = Input::all();
        $category = new Term;
        if($category->saveCategory($data)){
            return Redirect::route('admin_posts_categories')->withSuccess('Category  successfully added.');
        }else{
            return Redirect::route('admin_posts_categories')->withError('Failed to add category. #ErrorCode : 13');
        }
    }

术语模型:

public function saveCategory($data){
    $this->name = $data['name'];
    $this->slug = $data['slug'];
    if($this->save()){
        $category_taxo = new TermTaxonomy;
        $category_taxo->term_id = $this->lastCategoryId();
        $category_taxo->taxonomy = 'category';
        $category_taxo->description = $data['description'];
        if($category_taxo->save()){
            return true;
        }else{
            return false;
        }
    }else{
            return "#Error Code : 4";
    }
}

如下仅插入数据但随后显示空白页面且不重定向:

类别控制器:

public function store()
     {
         $data = Input::all();
         $category = new Term;
         $category->saveCategory($data);
    }

术语模型

public function saveCategory($data){
    $this->name = $data['name'];
    $this->slug = $data['slug'];
    if($this->save()){
        $category_taxo = new TermTaxonomy;
        $category_taxo->term_id = $this->lastCategoryId();
        $category_taxo->taxonomy = 'category';
        $category_taxo->description = $data['description'];
        if($category_taxo->save()){
            return redirect::route('admin_posts_categories')->withSuccess('Category successfully added.');
        }else{
            return redirect::route('admin_posts_categories')->withError('Failed to add category.');
        }
    }else{
            return redirect::route('admin_posts_categories')->withError('#Error Code : 4.');
    }
}

此外,我想问一些相关的问题,我的代码是否符合正确的设计模式,我应该将重定向放在哪里,在模型中还是在控制器中?

【问题讨论】:

  • 您绝对不应该将重定向逻辑放入您的模型中。如果你想学习更好的 Laravel 技术,我建议你访问 laracasts.com
  • 请不要将inline code formatting 用于跨越多行的代码块,因为在这些情况下它们会变得非常难看。改用 4 空格缩进的代码块,例如选择您的代码并单击编辑器工具栏中的 {} 图标。

标签: laravel laravel-4 laravel-routing


【解决方案1】:

首先,永远不要将重定向逻辑放入模型中。模型用于放置业务逻辑。第二件事检查您是否在 route.php 中为 admin_posts_categories 创建了路由,以及您如何调用视图。如果可能,请发布您有问题的路线代码。

【讨论】:

  • "从不将重定向逻辑放入模型" +1
【解决方案2】:

试试这个重定向:

1. return Redirect::back()->withSuccess('Category successfully added.');

2. return Redirect::to(URL::to('admin_posts_categories'))->withSuccess('Category successfully added.');

在 Controller 中添加重定向登录。即使你想放入模型(不推荐)使用 Ardent Hook 函数,即 afterSave()。

【讨论】:

  • 您的第一个解决方案按预期工作,它将重定向回之前的路线。但是您的第二个解决方案似乎无法正常工作,因为如果我将 admin_posts_categories 更改为其他命名路由,它仍会重定向回先前的路由而不是我想要的命名路由。无论如何,我应该使用模型到业务逻辑,然后给它返回 true 或 false ,以及重定向功能的控制器。对不起,我是 laravel 的新手。并感谢您的纠正。
  • 很高兴为您提供帮助,但我认为路线设置不正确或其他原因,因为我在项目中使用了这两个语句并且它们运行良好,无论如何干杯!
【解决方案3】:

我建议不要在您的模型中添加重定向。所以第一个解决方案将是您拥有的两个中最好的。

但回到你的问题。它显示一个空白页面,因为您的商店功能没有返回任何内容。 return $category->saveCategory($data); 但如前所述,这种方法不是最佳实践。

一个很好的建议是查看Laracasts,它会教你所有你知道的、不知道的以及关于 Laravel 的更多信息。

【讨论】:

    猜你喜欢
    • 2016-06-07
    • 1970-01-01
    • 2022-07-30
    • 2020-01-30
    • 2019-10-23
    • 2018-02-10
    • 2014-07-06
    • 2023-01-13
    • 1970-01-01
    相关资源
    最近更新 更多