Get URL paths

0

The API is structured something like 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 configure this url for production, homolog and localhost environments so I do not have to change all the time? And is there a better way to get this {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;
};
    
asked by anonymous 31.07.2015 / 13:52

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);
}
    
31.07.2015 / 14:24
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 go through and let it try to identify.

    
31.07.2015 / 14:22
0

There are n ways, the best I consider for sure is to virtualize the host of your site (vhost) and create a constant for each virtualized environment, keeping constant output by default (by editing the file (in linux): /etc/apache2/sites-available/seusite.conf and putting it in 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 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 in Windows

%systemroot%\windows32\drivers\etc\hosts

In 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 should be accessed in the browser with link , if you type only yourite.local will not scroll. Then you create a file in your config folder, named: application.ini . Inside it 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 load your file into 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: link
Is here: link

If you use some version driver like git or svn, you can create the same file as application.ini.template and put it in the versioning, in the case of git, just create a .gitignore in the root with the path: /config/application.ini and in 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 you just have to call your Controller from AngularJS, 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 being passed is from the same server, you do not need to deploy the host, just get the direct path 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"));
    };
  });
]);
    
31.07.2015 / 15:30