Skip to content

Differences between CanCan and AuthorityController

Tortue Torche edited this page Sep 28, 2013 · 1 revision
  • RESTful action names are a bit different between Rails and Laravel :
    • new action is create
    • create action is store
  • In Laravel, unlike Rails, by default id parameter of a Product resource is products. And shop_id parameter of a Shop parent resource is shops. So we need to reaffect correct parameter name(s) before executing a controller action. See ControllerAdditions::callAction().
  • In PHP all properties, methods and option names are in camelCase ($myProduct->myCamelCaseMethod();), in Ruby they are in snake_case.
  • Some methods don't have exactly the same name:
    • Ability#alias_action => Authority::addAlias
    • Ability#can => Authority::allow
    • Ability#cannot => Authority::deny
    • Ability|Controller#can? => Authority|Controller::can
    • Ability|Controller#cannot? => Authority|Controller::cannot
    • Ability|Controller#authorize! => Authority|Controller::authorize
  • In Ruby (with ActiveSupport) getter, setter, bool(true/false) methods are writing like this:
class Product
  # Pro tips: You can write "attr_accessor :name" instead of the two methods declarations below
  def name=(value)
    @name = value
  end

  def name
    @name
  end

  def named?
    @name.present?
  end
end

my_product = Product.new
# Setter method
my_product.name = "Best movie"
# Getter method
my_product.name #=> "Best movie"
# Check if my_product is set
my_product.named? #=> true
  • In PHP getter, setter, bool(true/false) methods are writing like this:
<?php
class Product
{
    protected $name;

    public function setName($value)
    {
        $this->name = $value;
    }
    public function getName()
    {
        return $this->name;
    }

    public function isNamed()
    {
        return !!$this->name;
    }
}

$myProduct = new Product;
// Setter method
$myProduct->setName("Best movie");
// Getter method
$myProduct->getName(); //=> "Best movie"
// Check if $myProduct is set
$myProduct->isNamed(); //=> true
Clone this wiki locally