Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I mix AngularJS with a PHP framework? [closed]

People also ask

Can I use AngularJS with PHP?

You will get introduced to AngularJS, the JavaScript framework created by Google, covering all the basics. You will learn how to build a simple website with PHP 7, MySQL and AngularJS 1.6+ from scratch, next you'll learn how to build a CRUD web application with PHP 7 API on the back-end and AngularJS on the front-end.

Which is better AngularJS or PHP?

AngularJS vs PHP PHP is largely based on back end coding which depends on heavy server-side scripting. Data management like inserting, updating and deleting the data is easier with PHP. PHP is still common among the developers but when it comes to presenting the product, AngularJS is more preferred.

Is it necessary to use a framework in PHP?

Why Should we use a PHP Framework? Developers should utilize PHP frameworks for various reasons, but the number one reason is for speeding up the development process. Reusing code across similar projects will save the developer a substantial amount of time and effort.

Which framework is better for PHP?

CodeIgniter is the top choice for a beginner-friendly PHP framework that is easy to use and quick to learn. It works on the MVC architecture, is easy to install, offers several guides and is easy to understand for beginners to get started with developing web applications on PHP.


It seems you may be more comfortable with developing in PHP you let this hold you back from utilizing the full potential with web applications.

It is indeed possible to have PHP render partials and whole views, but I would not recommend it.

To fully utilize the possibilities of HTML and javascript to make a web application, that is, a web page that acts more like an application and relies heavily on client side rendering, you should consider letting the client maintain all responsibility of managing state and presentation. This will be easier to maintain, and will be more user friendly.

I would recommend you to get more comfortable thinking in a more API centric approach. Rather than having PHP output a pre-rendered view, and use angular for mere DOM manipulation, you should consider having the PHP backend output the data that should be acted upon RESTFully, and have Angular present it.

Using PHP to render the view:

/user/account

if($loggedIn)
{
    echo "<p>Logged in as ".$user."</p>";
}
else
{
    echo "Please log in.";
}

How the same problem can be solved with an API centric approach by outputting JSON like this:

api/auth/

{
  authorized:true,
  user: {
      username: 'Joe', 
      securityToken: 'secret'
  }
}

and in Angular you could do a get, and handle the response client side.

$http.post("http://example.com/api/auth", {})
.success(function(data) {
    $scope.isLoggedIn = data.authorized;
});

To blend both client side and server side the way you proposed may be fit for smaller projects where maintainance is not important and you are the single author, but I lean more towards the API centric way as this will be more correct separation of conserns and will be easier to maintain.