Answer the question
In order to leave comments, you need to log in
How to make Yii2 (PHP) work with PHPSESSID when developing locally with Vue and Webpack?
Essence. Still wanting to get simple Yii working with Vue. That is, do not write rest, do not use jwt with refresh tokens, do not set up subdomains. Etc. In this case, it's just a website and nothing more. At the same time, I would like to use webpack to immediately update the page after a change in the source code and other goodies. It's about local development. Currently php (yii2) is spinning in docker at
127.0.0.1:8000 .
The vue project ( quasar.js ) is a completely separate application - localhost:8080/# After the build, everything is assembled, copied to the right place in the Yii project, integrated into the base view . Yii does not notice that it is fooled, does not use its frontend with its pensioner forms and signs on the bootstrap, everything works as it should.
However, devServer webpack, although it launches the application, updates the code promptly, Alas, it does not receive a session in the application in php. The session is simply not created in the application. Of course, I tried setting up a proxy in webpack:
proxy: {
'/': {
target: 'http://127.0.0.1:8000',
pathRewrite: {'^/': ''},
changeOrigin: true,
secure: false,
}
Cookie: XDEBUG_SESSION=PHPSTORM; csrftoken=pGnICOt7JvtQWQCJH3IBTuVti4BY9zwmi9d9Ip8WnY7q5iStlin4gPvxIcFa1kqu; _ga=GA1.1.1833168217.1577004843; gpr=true; _csrf=721KYaxSy6rOCGu9rLf30vfETmVrHkQU
Answer the question
In order to leave comments, you need to log in
Solution: (I hope :) . )
During the build process, we place the ready-made css, fonts, js, statics folders in the web yii folder. The Yii index.php file should also be present there. The content of the resulting index.html build is placed in one of the empty Yii views. You also need to add <?= Html::csrfMetaTags() ?> there.
All of the above operations occur automatically during the build process and are performed by webpack. There, in general, you can write whatever you want. Copying, adding code to files And so on. The view we created is opened when our application starts.
After the above manipulations, in the body of the page where the SPA is located, there will be meta tags with srf :
= Html::csrfMetaTags()
further, before starting Vue (for example in mounted() {.....} ) you need to extract the csrf from the page's meta tags and add it to axios.defaults.headers.common
This will work for an application that works in the usual way. The webpack version doesn't know anything about <?= Html::csrfMetaTags() ?> . Accordingly, we write a getToken action that returns csrf to us. This is necessary for development, so that the application works with webpack and its goodies.
public function actionGetToken()
{
return yii:: $app->getRequest()->getCsrfToken();
}
Set an intersector for Achios on requests.
If there is no cookie you need, refer to the page that contains only the session start method, for example, login.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question