0
votes

Using mix phoenix.gen.html, I created a new Ecto model and associated routes, and added the resources "/users", UserController to my router.ex. As expected, this autogenerated urls like /users/:id, /users/:id/edit, etc.

I'd like to change the URL structure to something like /users/:slug/:id/, where slug is a field on the User model. Is there a way to specify this new URL structure while still using the Phoenix resource routes?

If it makes things easier: I don't particularly care about validating :slug, or ensuring it's correct if the client makes a typo. I just want Phoenix to accept/generate URLs with that structure.

1

1 Answers

-2
votes

Let's say I have a several users grouped into teams. And I want to access a particular user from a team.

I can add this to router.ex

    get "/:team_name/:user_name", ProfileController, :show_in_team

In profile_controller.ex, I add

    def show_in_team(conn, %{"team_name" => team_name, "user_name" => user_name}) do
        render conn, "show.html", user_name: user_name, team_name: team_name
    end

Now I can access that particular user using localhost:4000/team1/user1