When vue is deployed on the server side, we all know that the dist file packaged through the npm run build command can be browsed directly by specifying it through http. Thinkphp can only be browsed by pointing to the index.php file through the domain name. To enable the front end to call the back end data normally.

There are two methods:
#1. The front-end calls the back-end data across domains .
2. The front-end packaging file is deployed in the back-end server folder (same domain).
Web server: apache
For example: cross-domain
Configure the site on the server:
在路径/home/www/ 下创建test项目文件夹,用来放项目文件。 找到httpd-vhosts.conf文件配置站点 前端站点: <virtualhost> ServerName test.test.com DocumentRoot "/home/www/test/dist" DirectoryIndex index.html </virtualhost> 后端站点: <virtualhost> ServerName test.testphp.com DocumentRoot "/home/www/test/php" DirectoryIndex index.php </virtualhost>
Package the front-end The good dist file is placed in the /home/www/test/ folder and can be browsed by running http://test.test.com. When the path changes, a 404 error will appear when refreshing. At this time, create an .htaccess file under the dist file. When the path does not exist, pointing the path to http://test.test.com/index.html can solve this problem.
<ifmodule>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</ifmodule>
Create the project root directory php folder in the /home/www/test folder, and place the thinkphp file under php. The entry file of TP5 is under the public file. Here, move the entry file index.php under public to the php folder (my personal habit is to put the entry file in the project root directory), and bind the Index module to the backend.
The front-end calls the back-end interface, there is cross-domain, and there are several cross-domain solutions. Here I will configure the back-end php to solve the cross-domain problem, and set the cross-domain configuration in the public controller:
class Common extends Controller
{
public $param;
// 设置跨域访问
public function _initialize()
{
parent::_initialize();
isset($_SERVER['HTTP_ORIGIN']) ? header('Access-Control-Allow-Origin: '.$_SERVER['HTTP_ORIGIN']) : '';
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, authKey, sessionId");
$param = Request::instance()->param();
$this->param = $param;
}
}
The front-end calls the login interface: this.axios.post('http://test.testphp.com/index.php/base/login', {user: '', password: ''}) .
(The interface can be defined under the webpack.base.conf.js file: http://test.testphp.com/index.php/)
Same domain
The backend configuration is the same as above, and the header configuration annotation in the public configurator is the same. Place all files under the front-end dist file (including .htaccess) in the php folder. Redirect the path of the index method of the back-end index controller to the index.html file under PHP:
namespace app\index\controller;
use think\Controller;
class Index extends Controller
{
public function index() {
$this->redirect('/index.html');
}
The front-end calls the login interface: this.axios.post('/index.php/base/login', {user: '', password: ''})
For more Thinkphp related technical articles, please visit the Thinkphp tutorial column to learn!
The above is the detailed content of How to combine vuejs with thinkphp. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Chinese version
Chinese version, very easy to use






