I am a frontend JS dev and I’ve begun working on a project that has a PHP backend running with Yii2 framework. The project came with no setup instructions so I’m trying to figure it out as I go, but coming from Node.js… it’s pretty confusing.
I downloaded and installed WAMP, created the MySQL DB for the API using MyPHPAdmin, and imported the data from a dump. That worked and I can see the tables and data. Now when I go to localhost
I see that WAMP is running. I tried the sample Yii project and that also runs at localhost/yii-test/web
.
According to Yii I am to serve this API by moving this entire API directory to the /www
directory of the WAMP install. Now if I go to localhost/fania-api
I see the directory listing for my server. To me this says that the API is accessible locally.
I have configured the .env
of my frontend to point to localhost/fania-api
, but when I send any request I get a CORS error, which I am assuming is masking some 404 or 500 error. Now the funny thing is the .env I was given comes with the URL http://fanintelligence.localhost
, not the Yii given localhost/fania-api
.
So my question is how do these two things fit together? I installed WAMP and setup the MySQL DB at the default port, but how does my API know it’s there? How can I know if it’s the right port? And what about these two different URLs, how do they match up?
Also the API does not contain a /web folder, not sure if that is an issue or not but I couldn’t serve it with php yii serve
either. If this was a Node.js server I’d clone, npm install
and npm run start
which would probably do something like build
and serve
. I can’t believe this is so difficult to setup in comparison.
Any advice or help is greatly appreciated!
EDIT: When I try to reach my server from the frontend at either address I get:
Access to fetch at 'http://localhost/fania-api/oauth2/token?accessToken=undefined' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
/fania-api/oauth2/token?accessToken=undefined:1 Failed to load resource: net::ERR_FAILED
However I do not believe this is an actual CORS error as the previous developer also used the local to test, so the CORS policy will allow for this. My guess is the address I’m pointing to is incorrect or the server is not actually live.
If I try to run php yii serve
:
Document root "C:Usersheseldevrepoeartothegroundfania-apiconsole/web" doe
s not exist.
But the crux of the erroneous behaviour is the inability to connect to a supposedly live local server (is it even live? how can I tell?).
Advertisement
Answer
I got it working. For hosting with WAMP:
Place entire server code folder in
www/
(in WAMP install dir)Edit
wamp64binapacheapache{version}confhttpd.conf
:
DocumentRoot "${INSTALL_DIR}/www/{your project}/{optional public folder}" <Directory "${INSTALL_DIR}/www/{your project}/{optional public folder}">
- Edit
wamp64binapacheapache{version}confextrahttpd-vhosts.conf
:
<VirtualHost {project name}:80> ServerName {project name}.localhost ServerAlias {project name}.localhost DocumentRoot "${INSTALL_DIR}/www/{your project}/{optional pub" <Directory "${INSTALL_DIR}/www/"> Options +Indexes +Includes +FollowSymLinks +MultiViews AllowOverride All Require local </Directory> </VirtualHost>
Your server should now be live at {project name}.localhost
.