Frontend Development 6 min read

Implementing Frontend‑Backend Separation with PHP (ThinkPHP6) and Vue.js

This tutorial demonstrates how to build a decoupled web application by creating RESTful APIs with PHP using ThinkPHP6 and a Vue.js frontend that consumes those APIs, covering code examples for controllers, components, routing, and integration.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Implementing Frontend‑Backend Separation with PHP (ThinkPHP6) and Vue.js

In modern web development, separating the frontend and backend has become a common practice, allowing independent development, deployment, and maintenance. This guide shows how to achieve this separation using PHP (ThinkPHP6) for the backend API and Vue.js for the frontend UI.

1. PHP Backend API with ThinkPHP6

ThinkPHP6 provides rapid API development. An example controller <?php namespace app\api\controller; use think\facade\Db; use think\facade\Request; class UserController { // Get user information public function getUserInfo($id) { $user = Db::name('user')->find($id); return json($user); } // Create a new user public function createUser() { $data = Request::param(); Db::name('user')->insert($data); return json(['message' => 'User created successfully']); } } implements two endpoints: one to retrieve a user by ID and another to create a user from POST data.

2. Vue.js Frontend Component

A Vue component can request the API and display the data. Example code:

<code>&lt;template&gt;
  &lt;div&gt;
    &lt;h1&gt;User Info&lt;/h1&gt;
    &lt;div v-if="loading"&gt;Loading...&lt;/div&gt;
    &lt;div v-else&gt;
      &lt;div&gt;Name: {{ user.name }}&lt;/div&gt;
      &lt;div&gt;Email: {{ user.email }}&lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/template&gt;
&lt;script&gt;
import axios from 'axios';
export default {
  data() {
    return { user: {}, loading: true };
  },
  mounted() {
    axios.get('/api/userInfo/' + this.$route.params.id)
      .then(response => {
        this.user = response.data;
        this.loading = false;
      })
      .catch(error => console.log(error));
  }
};
&lt;/script&gt;
</code>

This component fetches user information from the backend when mounted and displays it, showing a loading state until the data arrives.

3. Frontend‑Backend Integration

The Vue app uses axios to call the API endpoints. Routing is handled with vue-router :

<code>import { createRouter, createWebHistory } from 'vue-router';
import UserInfo from './components/UserInfo.vue';
import CreateUser from './components/CreateUser.vue';
const router = createRouter({
  history: createWebHistory(),
  routes: [
    { path: '/user/:id', name: 'UserInfo', component: UserInfo },
    { path: '/user/create', name: 'CreateUser', component: CreateUser }
  ]
});
</code>

Corresponding backend routes are defined in ThinkPHP6:

<code>use think\facade\Route;
Route::get('/userInfo/:id', 'api/UserController/getUserInfo');
Route::post('/user', 'api/UserController/createUser');
</code>

These routes map HTTP requests to the controller methods shown earlier, enabling the Vue frontend to retrieve and create user data via axios.get and axios.post .

4. Complete Example

The article concludes with a full example that combines the ThinkPHP6 controller, Vue component, router configuration, and API route definitions, illustrating a functional front‑end/back‑end separated application that is flexible, efficient, and maintainable.

Vue.jsweb developmentPHPFrontend‑Backend SeparationThinkPHP6
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.