2017-07-08 3 views
1

Ich brauche zwei Pipelines in meiner web/router.ex Datei wie folgt zu definieren:Wie kann eine Router-Pipeline-Definition in einer anderen Pipeline-Definition in Phoenix Framework wiederverwendet werden?

pipeline :api do 
    plug :accepts, ["json"] 
    plug :fetch_session 
    plug MyApp.Plugs.ValidatePayload 
end 

pipeline :restricted_api do 
    plug :accepts, ["json"] 
    plug :fetch_session 
    plug MyApp.Plugs.ValidatePayload 
    plug MyApp.Plugs.EnsureAuthenticated 
    plug MyApp.Plugs.EnsureAuthorized 
end 

Man kann deutlich sehen, dass Schritte von der :api Pipeline in der Pipeline :restricted_api dupliziert werden.

Gibt es eine Möglichkeit, die Pipeline :api in der Pipeline :restricted_api wiederzuverwenden?

Ich denke an etwas ähnlichen Erbe:

pipeline :api do 
    plug :accepts, ["json"] 
    plug :fetch_session 
    plug MyApp.Plugs.ValidatePayload 
end 

pipeline :restricted_api do 
    extend :api 
    plug MyApp.Plugs.EnsureAuthenticated 
    plug MyApp.Plugs.EnsureAuthorized 
end 

Antwort

3

Die pipeline Makro erstellt einen Funktionsstecker. Daher kann es in anderen Pipelines wie jeder andere Stecker mit plug :pipeline verwendet werden. Im mitgelieferten Beispiel:

pipeline :api do 
    plug :accepts, ["json"] 
    plug :fetch_session 
    plug MyApp.Plugs.ValidatePayload 
end 

pipeline :restricted_api do 
    plug :api 
    plug MyApp.Plugs.EnsureAuthenticated 
    plug MyApp.Plugs.EnsureAuthorized 
end 
Verwandte Themen