W3cubDocs

/Padrino

Module: Padrino::Routing::ClassMethods

Overview

Class methods responsible for enhanced routing for controllers.

Instance Method Summary

Instance Method Details

#absolute_url(*args) ⇒ Object

Returns absolute url. By default adds 'localhost' before generated url. To change that `set :base_url, 'example.com'` in your app.

#add_filter(type, &block) ⇒ Object

Adds a filter hook to a request.

#after(*args, &block) ⇒ Object

Add an after filter hook.

See Also:

#before(*args, &block) ⇒ Object

Add a before filter hook.

See Also:

#compiled_router ⇒ Object

#construct_filter(*args, &block) ⇒ Object

Creates a filter to process before/after the matching route.

Examples:

We are be able to filter with String path
before('/') { 'only to :index' }
get(:index} { 'foo' } # => filter match only before this.
get(:main) { 'bar' }
is the same of
before(:index) { 'only to :index' }
get(:index} { 'foo' } # => filter match only before this.
get(:main) { 'bar' }
it works only for the given controller
controller :foo do
  before(:index) { 'only to for :foo_index' }
  get(:index} { 'foo' } # => filter match only before this.
  get(:main) { 'bar' }
end

controller :bar do
  before(:index) { 'only to for :bar_index' }
  get(:index} { 'foo' } # => filter match only before this.
  get(:main) { 'bar' }
end
if filters based on a symbol or regexp
before :index, /main/ do; ... end
# => match only path that are  +/+ or contains +main+
filtering everything except an occurrence
before :except => :index do; ...; end
you can also filter using a request param
before :agent => /IE/ do; ...; end
# => match +HTTP_USER_AGENT+ containing +IE+

Parameters:

  • args (Array)

See Also:

#controller(*args) { ... } ⇒ Object Also known as: controllers

Method to organize our routes in a better way.

In a controller, before and after filters are scoped and don't

affect other controllers or the main app.

In a controller, layouts are scoped and don't affect other

controllers or the main app.

Examples:

controller :admin do
  get :index do; ...; end
  get :show, :with => :id  do; ...; end
end

url(:admin_index) # => "/admin"
url(:admin_show, :id => 1) # "/admin/show/1"
Using named routes follow the sinatra way:
controller "/admin" do
  get "/index" do; ...; end
  get "/show/:id" do; ...; end
end
Supply :provides to all controller routes:
controller :provides => [:html, :xml, :json] do
  get :index do; "respond to html, xml and json"; end
  post :index do; "respond to html, xml and json"; end
  get :foo do; "respond to html, xml and json"; end
end
Specify parent resources in padrino with the :parent option on the controller:
controllers :product, :parent => :user do
  get :index do
    # url is generated as "/user/#{params[:user_id]}/product"
    # url_for(:product, :index, :user_id => 5) => "/user/5/product"
  end
  get :show, :with => :id do
    # url is generated as "/user/#{params[:user_id]}/product/show/#{params[:id]}"
    # url_for(:product, :show, :user_id => 5, :id => 10) => "/user/5/product/show/10"
  end
end
Specify conditions to run for all routes:
controller :conditions => {:protect => true} do
  def self.protect(protected)
    condition do
      halt 403, "No secrets for you!" unless params[:key] == "s3cr3t"
    end if protected
  end

  # This route will only return "secret stuff" if the user goes to
  # `/private?key=s3cr3t`.
  get("/private") { "secret stuff" }

  # And this one, too!
  get("/also-private") { "secret stuff" }

  # But you can override the conditions for each route as needed.
  # This route will be publicly accessible without providing the
  # secret key.
  get :index, :protect => false do
    "Welcome!"
  end
end
Supply default values:
controller :lang => :de do
  get :index, :map => "/:lang" do; "params[:lang] == :de"; end
end
controller :posts do
  layout :post
  before { foo }
  after  { bar }
end

Parameters:

  • args (Array) — Controller arguments.

Yields:

  • The given block will be used to define the routes within the Controller.

#deferred_routes ⇒ Object

#delete(path, *args, &block) ⇒ Object

#get(path, *args, &block) ⇒ Object

#head(path, *args, &block) ⇒ Object

#options(path, *args, &block) ⇒ Object

#parent(name = nil, options = {}) ⇒ Object

Provides many parents with shallowing.

Examples:

controllers :product do
  parent :shop, :optional => true, :map => "/my/stand"
  parent :category, :optional => true
  get :show, :with => :id do
    # generated urls:
    #   "/product/show/#{params[:id]}"
    #   "/my/stand/#{params[:shop_id]}/product/show/#{params[:id]}"
    #   "/my/stand/#{params[:shop_id]}/category/#{params[:category_id]}/product/show/#{params[:id]}"
    # url_for(:product, :show, :id => 10) => "/product/show/10"
    # url_for(:product, :show, :shop_id => 5, :id => 10) => "/my/stand/5/product/show/10"
    # url_for(:product, :show, :shop_id => 5, :category_id => 1, :id => 10) => "/my/stand/5/category/1/product/show/10"
  end
end

Parameters:

  • name (Symbol) (defaults to: nil) — The parent name.
  • options (Hash) (defaults to: {}) — Additional options.

#patch(path, *args, &block) ⇒ Object

#post(path, *args, &block) ⇒ Object

#process_path_for_parent_params(path, parent_params) ⇒ Object

Processes the existing path and prepends the 'parent' parameters onto the route Used for calculating path in route method.

#put(path, *args, &block) ⇒ Object

#rebase_url(url) ⇒ Object

#recognize_path(path) ⇒ Symbol, Hash

Recognize a given path.

Examples:

Giving a controller like:
controller :foo do
  get :bar, :map => 'foo-bar-:id'; ...; end
end
You should be able to reverse:
MyApp.url(:foo_bar, :id => :mine)
# => /foo-bar-mine
Into this:
MyApp.recognize_path('foo-bar-mine')
# => [:foo_bar, :id => :mine]

Parameters:

  • path (String) — Path+Query to parse

Returns:

  • (Symbol, Hash) — Returns controller and query params.

#reset_router! ⇒ Object

#router ⇒ Object Also known as: urls

Using PathRouter, for features and configurations.

Examples:

router.add('/greedy/:greed')
router.recognize('/simple')

#url(*args) ⇒ Object Also known as: url_for

Instance method for url generation.

Examples:

url(:show, :id => 1)
url(:show, :name => 'test', :id => 24)
url(:show, 1)
url(:controller_name, :show, :id => 21)
url(:controller_show, :id => 29)
url(:index, :fragment => 'comments')

Parameters:

  • options (Hash) — a customizable set of options