2017-03-15 1 views
1

ドキュメントにはこれはまったく言及されていません。それはネストスコープの定義が許可され、そうであればウェブ/ router.exで、次のために定義された動作が何である:ロケール/認証要求の経路で:web/router.ex内のネストされたスコープ定義の動作

scope "/:locale", MyApp do 
    pipe_through [:browser, :locale] 

    scope "/auth", MyApp do 
    pipe_through [:auth] 

    get "/", PageController, :dummy 
    end 

    get "/", PageController, :dummy 
end 

は、パイプのチェーンを実行し、/は.IEのとおりです。ブラウザ、:ロケール、:認証?どんな怪物?

答えて

1

はい、スコープの定義が許可され、パイプラインはネストされたスコープによって継承されます。 Phoenix repo contains tests for pipelines in nested scopesは、親スコープ内のすべてのpipe_throughが子スコープ内のルートによって継承されることを主張します。

scope "/browser" do 
    pipe_through :browser 
    get "/root", SampleController, :index 

    scope "/api" do 
    pipe_through :api 
    get "/root", SampleController, :index 
    end 
end 

# ... 

test "invokes pipelines in a nested scope" do 
    conn = call(Router, :get, "/browser/api/root") 
    assert conn.private[:phoenix_pipelines] == [:browser, :api] 
    assert conn.assigns[:stack] == "api" 
end 
1

は、次のルート

# The following route has the :browser and :locale plugs 
/:locale/  # Points to MyApp.PageController.dummy 

# The following route has the :browser, :locale and :auth plugs 
/:locale/auth/ # Points to MyApp.MyApp.PageController.dummy 

あなたが実際にMyApp.MyApp.PageControllerを指すようにしたくないようにエイリアスを指定せずにscope/2を定義することができますので、私は、感じ

を持つことになります。

scope "/:locale", MyApp do 
    pipe_through [:browser, :locale] 

    scope "/auth" do 
    pipe_through [:auth] 

    get "/", PageController, :dummy 
    end 
end 

これで認証ルートがMyApp.PageControllerに変更されます。

関連する問題