Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current url in phoenix framework

I would like to know the current url with elixir / phoenix framework, how I can get this ?

Edit #1:

My nginx config file:

server {
        client_max_body_size 100M;

        listen  80;

        server_name *.babysittingbordeaux.dev *.babysittingparis.dev

        access_log  /usr/local/var/log/nginx/baby-access.log;
        error_log   /usr/local/var/log/nginx/baby-error.log;


        location / {
            proxy_pass http://127.0.0.1:4000;
        }
}

Code:

Atom.to_string(conn.scheme) <> "://" <> (Enum.into(conn.req_headers, %{}) |> Map.get("host")) <> conn.request_path

That example returns http://127.0.0.1:4000/, I would like to get http://www.babysittingbordeaux.dev/

I'm in development mode.

like image 406
Jeremie Ges Avatar asked Jun 09 '16 20:06

Jeremie Ges


People also ask

Is Phoenix framework fast?

For any company that currently uses Ruby on Rails, Phoenix Framework should be on your radar, because it adds considerable performance gains. To top that off, Phoenix makes it incredibly fast to build web applications.

What is Phoenix framework used for?

Phoenix is a web framework built with the Elixir programming language. Elixir, built on the Erlang VM, is used for building low-latency, fault-tolerant, distributed systems, which are increasingly necessary qualities of modern web applications.

Is Phoenix a good framework?

Awesome Phoenix web framework Phoenix framework is where Elixir shines. It's built-in a very elegant manner and gives you everything you need to build robust web applications as well as advanced APIs.

How to get current site URL of SharePoint site in SPFX?

You need to get the current site URL of a SharePoint site in SPFx. In JavaScript, we do it using _spPageContextInfo.webAbsoluteUrl. To do the same in SPFx we have to follow the below steps ? using this props, you can get the current site URL where your webpart is being rendered.

How to get the current URL in Magento 2?

With Magento 1 version, it was very easy to get the base URL using Mage::getBaseUrl (); However, it is not easy in Magento 2. You can follow the two methods to Magento to get the current URL: Although the 2nd solution is not recommended as it consumes extensive resources, it does provide the solution and can be used as an alternative.

How do I work with links and URLs in HTML?

Conveniences for working with links and URLs in HTML. Generates a button tag that uses the Javascript function handleClick () (see phoenix_html.js) to submit the form data. Generates a link to the given URL. Generates a button tag that uses the Javascript function handleClick () (see phoenix_html.js) to submit the form data.


2 Answers

You can use Phoenix.Controller.current_url/1:

current_url(conn)

The endpoint configuration will define the URL:

config :my_app_web, MyAppWeb.Endpoint, url: [host: "example.com"]
like image 114
Dan Avatar answered Oct 27 '22 03:10

Dan


If you are interested only in the request path you can use conn.request_path which holds a value like "/users/1".

To get the URL including the host you can use

MyApp.Router.Helpers.url(conn) <> conn.request_path

which would return a result like "http://localhost:4000/users/1".

like image 39
ventsislaf Avatar answered Oct 27 '22 03:10

ventsislaf