Get URL paths

Asked

Viewed 366 times

0

The API is structured something similar to http://localhost:8181/api/collections/{id}, my question is: Sometimes I need to get this {id} and I’m doing a replace.
Is there any way to set up this url for production, Homolog and localhost environments so you don’t have to change all the time? And there’s a better way to get that {id}?

The way I get this {id}:

var oknokdev = "http://localhost:8181/";
$scope.visualizar = function (id) {
        id = id.replace(oknokdev + "api/veiculos/", "");
        window.location.href = "/#/info/" + id;
};

3 answers

1


Another way to get the id is by using split.

In a very simple way, using angular:

//http://localhost:8181/api/collections/{id}
angular.module("APP",[],function($locationProvider){
    $locationProvider.html5Mode(true);
});

function MainController($location){
    var id = $location.path().split("/")[3]||"0";    //path retorna /api/collections/{id}, e o array retorna: ["","api","collections","id",""]
    console.log(id);
}

0

Try to adapt this way:

$scope.visualizar = function (id, host){
    var r = null;
    if(typeof(host) != undefined){
        r = new RegExp(host+'/api/[^/]+/(.+)');
    }else{
        r = new RegExp('https?://[^/]+/api/[^/]+/(.+)');
    }

    id = id.replace(r, '$1');
    window.location.href = "/#/info/" + id;
};

This way you can specify the host through the second parameter, or simply not pass and let it try to identify.

  • Any doubt about how this works, comment I edit.

0

There is a n ways, the best I consider for sure is to virtualize your website host (vhost) and create a constant for in each virtualized environment, keeping the output of constant by default for production (editing the file (on linux): /etc/apache2/sites-available/seusite.conf and putting him on the list with the command: a2ensite seusite.conf, in Windows (xampp), just edit the file: C:/xampp/apache/config/extra/httpd-vhosts.config and restart the Apache.

<VirtualHost *:80>
        ServerName seusite.local
        ServerAlias www.seusite.local
        ServerAlias seusite_outronome.local
        #para xampp windows: DocumentRoot C:/xampp/htdocs/seuprojeto/
        DocumentRoot /var/www/html/seuprojeto/ 

        #--->desenvolvimento
        SetEnv APPLICATION_ENV "development"

        #--->homologação
        #SetEnv APPLICATION_ENV "staging"

        #--->testes
        #SetEnv APPLICATION_ENV "testing"

        #--->produção 
        #SetEnv APPLICATION_ENV "production"

    <Directory />
            DirectoryIndex index.php
            AllowOverride All
            Order allow,deny
            Allow from all
     </Directory>
</VirtualHost>

Remembering to enable your browser, just put the IP in sequence for each virtualized host on Windows

%systemroot%\windows32\drivers\etc\hosts

On Ubuntu linux:

/etc/hosts

And edit the paths:

127.0.0.1   localhost
127.0.1.1   seusite.local
127.0.2.1   seusite2.local
127.0.3.1   seusite3.local

Obs: These addresses must be accessed in the browser with http://seusite.place, if you type only your site.local will not roll. Then you create a file in your config folder called: application ini.. Inside put the variables for each environment:

[production]
;configurações comum a todos 
appnamespace = "Application"
adapter = "PDO_Mysql"
driver_options = "SET NAMES UTF8"
[development : production]
;aqui vem configurações de desenvolvimento

username = "root"
password = "senha"
dbname = "banco_local"
host = "localhost"
site = "http://www.seusite.com.br"

[testing : production]
;aqui vem configurações de testes

username = "root"
password = "senha"
dbname = "banco_testing"
host = "localhost"
site = "http://eusite.testing.local"

[staging : production]
;aqui vem configurações de homologação

username = "root"
password = "senha"
dbname = "banco_staging"
host = "localhost"
site = "http://homolog.seusite.com.br"

[development : production]
;aqui vem configurações de produção

username = "root"
password = "senha"
dbname = "banco_production"
host = "localhost"
site = "http://seusite.local"

Then you upload your file to your project:

$application = parse_ini_file('config/application.ini');
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));

define("DATABASE_SERVER", $application['host']);
define("DATABASE_USER", $application['username']);
define("DATABASE_PASS", $application['password']);
define("DATABASE_DB", $application['dbname']);
define("URL_SITE", $application['site']);

More information here: http://tr2.php.net/manual/en/function.parse-ini-file.php
And here: http://tr2.php.net/manual/en/function.getenv.php

If you use some version controller like git or svn, you can create the same file as application.ini.template and put it in versioning, in the case of git, just create a . gitignore at the root with the path: /config/application.ini and in the svn I believe it to be . cvsignore.

In case you could do something like this, in PHP:

$bool = $_POST['redirect'];
if ($bool) {
echo json_encode(array('url'=>URL_SITE)); die();
}

Now just call in your Angularjs Controller, remembering that you need to have the jQuery library in your view:

$scope.redirectUrl = function(strUrl) {
    $.post('houte.php',{redirect:true},function(data) {
           var host    = jQuery.parseJSON(data);
           var reg_exp = new RegExp(host['url'] + '/api\/(.*)\/(.+)');
           var id      = strUrl.replace(reg_exp, '$2');
           window.location.href = "/#/info/" + id;
       });
 };

If the URL that is being passed is from the same server, you do not need to implement the host, just take the path directly by passing its URL:

angularApp.controller('SeuController', ['$scope', '$routeParams', '$location', '$http',
    function ($scope, $routeParams, $location,  $http) {

    $scope.visualizar = function(pathUrl) {
     var reg_exp = new RegExp('/api\/(.*)\/(.+)');
        $location.path(pathUrl.replace(reg_exp,  "/info/$2"));
    };
  });
]);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.