How run Angular2 (front-end) and Symfony3 (back-en

2019-03-03 20:06发布

This is my first question here. I have already spent hours reading topics, but never posted a question before.

At this moment I am developping an Angular4 application. I am running my app with "ng build --watch" and a local PHP webserver pointed to the 'dist' folder of my angular app. (When using build-in liveload server 'ng serve' there isn't a PHP server available, so i fixed this with 'ng build --watch' and a local PHP build-in server of PHPStorm)

To communicate with my MYSQL database, I used before single-php files in a directory called '/api'. I added this folder to my assets in the angular-cli.json file, so the API folder is also being pushed to the 'dist' folder when running the app local.

In this case, I was able to use relative paths to point my http requests. (like a POST action to '/api/insert.php'). So when publishing my app, it was not necessary to modify the paths of my HTTP requests.

But now I would like to use a backend framework, after some research I found http://api-platform.com, a PHP framework builded on Symfony3. When i am running this API, this runs for example on localhost:8000 while my angular applciation runs on localhost:4200.

So, that means i would have to use absolute paths for my HTTP requests. (eq http://localhost:8000/api/insert.php instead of /api/insert.php).

At this moment I have 2 projects: one front-end and one back-end. To make developping faster, easier, and simpler I would like to put both projects together. I know some developpers don't like this way of architecture, but in my case it is the best solution, one project, with a front and back-end included.

Concrete: Is it possible to run a debug-webserver in development zone with my angular app running on eq localhost and my symfony3/api-platform on eq localhost/api on the same time, same domain, and same port?

I would like to keep this project folder structure to keep it clean: - projectname (root) --- frontend ----- (all directories/files from angular) --- backend ----- (all directories from api-platform / symfony3)

Sincerely,

Lander

2条回答
贼婆χ
2楼-- · 2019-03-03 20:17

I ended up by using another backend. In my case an nodeJS server was the best, fast and most simple solution.

查看更多
男人必须洒脱
3楼-- · 2019-03-03 20:20

running both application on the same domain (f.ex. localhost) is no problem, but they need to run on different ports. In your case angular is running on 4200 and your PHP application on 8000. You can configure angular with a proxy, which passes requests to http://localhost:4200/api to http://localhost:8000/api

In your angular root directory (where your package.json is), create a file proxy.conf.json

{
    "/api": {
        "target": "http://localhost:8000",
        "secure": false
    }
}

then change your package.json to use this proxy:

"scripts": {
    "ng": "ng",
    "start": "ng serve --proxy-config proxy.conf.json",
    // other scripts
}

now run the webpack server with npm start instead of ng serve

Personally, I git ignore the proxy.conf.json, because some colleagues run their backend on a different port. Therefore we created a proxy.conf.dist.json with the above contents in git. If you want to use a different port, you can just copy it and change the port, while those, who want to use the default, can simply symlink to it.

UPDATE:

We recently switched to another method, namely using environment configurations.

By default, Angular has a dev and prod environment, specified in the .angular-cli.json

"environments": {
    "dev": "environments/environment.ts",
    "prod": "environments/environment.prod.ts"
}

In these files, specify the API path:

environment.ts

export const environment = {
    production: false,
    apiUrl: '//localhost:8000'
};

environment.prod.ts

export const environment = {
    production: true,
    apiUrl: '//my-api-domain.tld'
};

You can add as many environments as you want (f.ex. for tests, beta, etc.). In angular, you can use the environment by importing it:

import {environment} from '../environments/environment';

But I suggest creating a config service following this answer: https://stackoverflow.com/a/43193574/7933618

查看更多
登录 后发表回答