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

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

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

Undress AI Tool
Undress images for free

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

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor