douou8954 2019-05-15 12:25
浏览 66

Vue.js与PHP

I have been using only JQuery in my app in order to make the Frontend a bit more interactive. Server-side, I am using PHP.

I've read about Vue.js lately, and am really interested by its reactive components, but I think I am missing some points in order to make a transition towards it.

I have a blog module that allows posts and comments. Since data of posts and comments are quite similar, I am using a PHP function to format them, using the instance of my Entity to separate some chunks of code. Looks like this :

function formatPost(Entity $post) {
    $html .= '<div class="post" data-item="'.$post->id().'">
                 <div class="toolbar">Edit / Delete / Report</div>
                 <div class="user">'.$post->user()->nickname().'</div>
                 <div class="date">'.$post->date().'</div>
                 <div class="content">'.$post->message().'</div>';

    if ($post instanceof BlogPost) {
        $html .= '<div class="favorite">Set as fav</div>'; // for example
    }
    elseif ($post instanceof Comment) {
        $html .= '<div class="upvote">Upvote</div>
                  <div class="downvote">Downvote</div>'; 
    }

    $html .= '</div>';

    return $html
}

What I want to do with Vue.js is to create a BlogPost component that contains all data and features you saw in the PHP function above.

So, I thought of rewriting the PHP function to JS at first, like this :

Vue.component('blogPost', {
  props: ['item', 'date', 'message', 'user', 'isComment'],
  template: '<div class="post">\
                 <div class="toolbar" v-on:click="doSomething">Edit / Delete / Report</div>\
                 <div class="user">{{ user }}</div>\
                 <div class="date">{{ date }}</div>\
                 <div class="content">{{ message }}</div>\

                 <template v-if="isComment">\
                    <div class="favorite"></div>\
                 </template>\
                 <template v-else>\
                    <div class="upvote"></div>\
                    <div class="downvote"></div>\
                 </template>\
             </div>';
});
<?php foreach ($blogPosts as $post) { ?>
  <blogPost item="<?= $post->id() ?>"
            date="<?= $post->date() ?>" 
            message="<?= $post->message() ?>"
            user="<?= $post->user()->nickname() ?>"
            isComment="<?= $post instanceof Comment ?>"
  >
  </blogPost>
<?php } ?>

This way it would be easy to apply the features I want, as such as the edit function that would only replace the message with some ajax, or the vote feature that would update the number of votes and such.

My problem is that my PHP function is actually way longer and complicated than what's shown here and it will be quite redundant to pass all the props to create the custom component since I don't know how to pass a PHP object to JS.

  <blogPost item="..."
            date="..." 
            message="..."
            user="..."
            profilePicture="..."
            votesNumber="..."
            favoritesNumber="..."
            repliesNumber="..."
            isComment="..."
            ...
  >
  </blogPost>

I then thought that I could use some parents and children components so it would separate my code in small pieces :

Vue.component('blogPost', {
  props: ['item'],
  template: '...'
});

Vue.component('edit', {
  template: '...'
});

Vue.component('delete', {
  template: '...'
});
<blogPost item="<?= $post->id() ?>">
  <edit></edit>
  <delete></delete>
  <!-- etc... -->
</blogPost>

But I need the blogPost's item prop to work with my edit and delete features, and I don't know how to pass it to the children.

I'm really confused with how to work Vue.js and PHP, could you guys please help me with that?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么