Asked  7 Months ago    Answers:  5   Viewed   39 times

AngularJS is very powerful when it comes to interactive HTML5 and model binding. On the other hand, PHP frameworks like Yii enable quick, well-structured, safe and powerful web application development. Both technologies provide sophisticated means for data access, iteration and page layouting.

Is it good or bad practice to mix those two approaches (client-side and server-side "page setup") or is this rather against the meaning of interactive, seamless HTML5 AJAX web applications?

I am not talking about generating JS using PHP (See this question) - I'm talking about generating a view that will make use of AngularJS.

I also know that an AngularJS page should (or can) communicate with the server via REST services to get data (See this question) instead of retrieving it from for example PHP variables directly. But to me it seems more convenient to design the "frame" for the entire web application separately in PHP (e.g. build the main menu or handle authorization/ sessions etc.)

 Answers

38

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.

Wednesday, March 31, 2021
 
treeface
answered 7 Months ago
63

Using a framework saves you a ton of time and helps you to organise your app mroe logically.

If the app is small then frameworks like CodeIgniter or Kohana are ideal.

If you are building a bigger app that needs to scale the Symfony or possiby Zend are the ones to look at.

If you were to start your app in Symfony you would instanly get:

  • url rewriting using routes
  • a form framework for handling everything to do with your web forms
  • internationalisation and localisation
  • emailing
  • caching
  • admin area generator, to help you add data quickly
  • unit testing framework

Plus the whole framework is extendable and custmisable to suit your needs.

Starting without a framework you'd have to write all of this yourself. Or integrate several disparate open source modules.

Hope this helps.

Wednesday, March 31, 2021
 
Karsten
answered 7 Months ago
68

You could do it like this:

function addLinkPost(array $postDetailArray)
{
    $fields = array(
        'key' => 'default value',
        'title' => 'Error: No Title',
    );

    foreach ($fields as $key => $default) {
        $$key = isset($postDetailArray[$key]) ? $postDetailArray[$key] : $default;
    }
}

Simply edit the $fields array with your key and its default value.

Wednesday, March 31, 2021
 
tplaner
answered 7 Months ago
19

MVC allows you to separate your business logic from your presentation layer. This "Separation of Concerns" allows you to quickly find and edit portions of your code. It also enables easy reuse of your UI components across your system.

Check out the wiki page for a overly academic and technical introduction to MVC http://en.wikipedia.org/wiki/Model_view_controller

Saturday, May 29, 2021
 
LOKESH
answered 5 Months ago
79

Have you looked up Flask?

It's a much more minimalistic framework, and very easy to set up and get started.

Tuesday, September 21, 2021
 
Geekuna Matata
answered 4 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :