Calling PHP files and functions via Angularjs

Asked

Viewed 273 times

1

Before going into the matter, I would like to clarify that I already have a working method. However, as I do not possess as much knowledge in backend, more precisely in PHP, I don’t know how "correct" my code is analyzing issues of security, scalability and good practices. I also don’t make use of any framework in backend.


The "problem"

After doing some research (unrelated to this particular problem), I came across some examples of users who were calling the functions in PHP (from AngularJS) in a different way than I do now. In fact, I did not find any example similar to what I do, so I came up with this question, because I may be "doing wrong".

The method I do would be this:

Angularjs

var url  = '/php/produtos.php?action=carregaProduto';
return $http.get(url).then(
    function(response) {return response.data;}
)

PHP - products.php

switch($_GET['action']) {
    case 'carregaCategoria': carregaCategoria();
    break;

    case 'carregaProduto': carregaProduto();
    break;
    
    case 'adicionaProduto': adicionaProduto();
    break;
}

function carregaCategoria() {
    //Executa a função
}
function carregaProduto() {
    //Executa a função
}
function adicionaProduto() {
    //Executa a função
}

I own some files php segmented by "categories", for example produtos.php, clientes.php, filtros.php, etc.. For each file I follow the same pattern, defining a marry to call the function.


The models

The models I observed of tutorials, have a slightly different structure, with the call of the function directly in url, but without action or without the definition of the function itself in url. They also use a structure PHP different from what I use, with the definition of class and Public function.

For example:

Angularjs

var url = '/produtos/carregaProduto';
return $http.get(url).then( 
    function(response) {return response.data;}
)

PHP - products.php

class listaProduto {
    public function carregaCategoria() {
        //Executa a função
    }
    public function carregaProduto() {
        //Executa a função
    }
    public function adicionaProduto() {
        //Executa a função
    }
}

As you can see, the call made in AngularJs mentions the file, but without the extension and the function is also defined, but without ().

Reading a little about it, I noticed that it would be necessary a configuration of "Routes" in PHP for this to work. This determines the pattern of routes and methods to be called.


My doubt

With this, my doubt would be as to the most "correct" or better method of obtaining the final result, which is to get data from the database, or perform some process in backend.

Is the method I use currently wrong? Is the method shown in the example the most correct one? Why?

Remembering that I want to analyze well the points I mentioned at the beginning of the issue (security, scalability and good practices). In the example I used few functions, but in a larger application, as an e-commerce, eventually I will have hundreds of functions and several files.

No answers

Browser other questions tagged

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