dongqian8265 2013-09-20 00:42
浏览 19
已采纳

我们可以在方法调用之前添加过滤器吗?

Take this Ruby on Rails example.

class SomeController < ApplicationController
  before_filter :generateRandomValue

  def generateRandomValue
     //generates a random value between 0 and 10
  end

  def getBoo
     //Return value generated by the method above
   end
end

If we call getBoo, class will run generateRandomValue first because it has a general scoped before filter.

We can also tweak this before filters in Ruby on Rails, like;

    method x,y,z runs before a method.
    method 1,2,3 runs before b,c,d method.
    method always, always runs. (think it like PHP's __construct())

Is there any way to set before filters before controller method calls in Laravel 4?

The main reason is, I want to DRY most of my code by applying before filters.

Thank you.

  • 写回答

1条回答 默认 最新

  • dongxietao0263 2013-09-20 02:57
    关注

    Yes - this is a new feature in Laravel 4.

    Taylor has a good video on it here you can watch - which shows it in action and the code to use.

    But in general you just add a filter to your constructor:

    Class ExampleController extends BaseController
    {
        public function __construct()
        {
            $this->beforeFilter('myfilter');        
            $this->beforeFilter('anotherfilter')->only('getBoo');       
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?