PHP backend inside vuejs project folder - php

I created a vue/cli 3 project and its in my documents/project folder. Im running it on port 3000 with "npm run serve" command. Now, i want to have a backend folder somewhere to serve PHP files. But i want both the frontend and backend to be in the same folder structure.
Currently, my vue is in my documents folder and my php is in my xampp folder. The problem im having with this is managing 2 folders and git for the same application. Also, this causes my ajax calls from my vue projet to use a silly url such a "http://localhost:80/project/file.php". Id like to use relative paths like "backend/clients/getClient.php" that would be somewhere within my vue project.
This is what i would like:
myProject
frontend
my vue stuff like public and src folders
backend
my php files
It doesnt have to be exactly that way but the point is that now i have a single folder and git for a single project.
thank you.

The way I do this is as follows:
1)
I put the php files in a subfolder the public/ folder in the vue/cli project structure.
That way, these files will get copied to the dist folder on building the project.
For example: I have the file /public/api/endpoint.php, this will end up in dist/api/endpoint.php.
For testing locally I now copy the php files to the xampp/htdocs dir whenever I change them. But I guess you could skip this step by setting up a xampp alias directory. (But I haven't tried this myself yet)
2)
The ajax urls still differ between local development mode and production. To solve this I define a webpack plugin in my vue.config.js:
const webpack = require('webpack')
const API_URL = {
production: JSON.stringify('https://www.project.com/api/endpoint.php'),
development: JSON.stringify('http://localhost/project/api/endpoint.php')
}
// check environment mode
const environment =
process.env.NODE_ENV === 'production' ? 'production' : 'development'
module.exports = {
...
configureWebpack: {
plugins: [
new webpack.DefinePlugin({
API_URL: API_URL[environment]
})
]
},
...
}
Then use this in a vue/js file like:
const apiUrl = API_URL // eslint-disable-line
(the eslint directive is only needed If you have linting turned on with eslint, it will throw an error that API_URL is not defined.)

Related

How using vuejs 3 with Wintercms or Octobercms

Until a while ago I was using this lib (https://github.com/scottbedard/vuetober) to use vuejs with octobercms/wintercms, however, in the new versions of Node, NPM and Wepback this lib doesn't work anymore, because the variables that are used to give the "output" of the compiled files no longer exist..
Would anyone have a solution to use Vue 3 in which I can create the files in the themes/my-theme/assets folder in the vue build action and also have an index.htm file in the "pages" folder, something similar to the lib above do?
From what I understand the new version of webpack no longer has access to the "public" variable, that's why the vuetober lib stopped working.
// configure the dev server and public path based on environment
options.devServer = {
disableHostCheck: true,
public: 'http://localhost:8080',
};
options.publicPath = isProduction
? getProductionBaseUrl(api, pluginOptions)
: 'http://localhost:8080/';

Laravel vue js deploy application on live

I have created a project on laravel using vue js. Where my frontend is totally based on vue js and i am using laravel apis for backend. My project structure is like :
--app
--bootstrap
--config
--database
--fontend //This is my vue js code
-- assets
-- layout
-- mode_modules
-- pages
-- public
-- resources
... and so on.
I have run this command on my frontend folder
npm run build
It created a folder dist in frontend, now what will be the next step ?
My Server Information Ubuntu (Apache)
You should just copy contents of dist folder on your server main folder, or of you have folder structure with folders like public/www/web, then there.
If you want to run "live" on wamp or xamp then copy to folder www.
Of course if you already have working backend
You just copy contents inside the dist folder from your xampp/wampp folder.
and on Ubuntu server place inside directory /var/www/html folder.
or if you want to place inside any folder on the server like /var/www/html/front
then on front folder place contents inside front folder then point domain to that front fodler.

How to setup up the folder structure of angular 2 app?

I have been following the Angular 2 Tour of Heroes tutorial and Quick Start guide on anuglar.io. In the tutorials all the of the files are directly under the app folder. I have started my own project and I want to set up the folder structure where I Have a components folder, services folder, and directives folder all under the app folder for the project. Under each of these folders I would also like to have folders the house each individual component to include the ts, js, map, and the html files relating to the component so for instance. It would look something like this:
-My_Project
- app
- components
-app
- app.component.ts
- app.component.js
- app.component.map
- app.component.html
-logon
- logon.component.ts
- logon.component.js
- logon.component.map
- logon.component.html
- services
-user
- user.services.ts
- user.services.js
- user.services.map
so on and so forth.
I understand about using absolute and relative paths and using ../ to move up to the parent folder and ../../ to go back two folders in the path, but for some reason it doesn't seem to be working with the lite server. This works just fine when i run the app with Windows IIS 7.5. When I try to run the compiler I get errors of the following nature:
app - Copy/app.component.ts(6,32): error TS2307: Cannot find module './logon/logon.component'.
I am using the following to reference the logon.component file from the app.component file.
import { LogonComponent } from '../logon/logon.component';
Also I have some PHP files in a php folder located in the Project folder. This again works fine when ran on Windows IIS 7.5 but when i try and run it with the lite server I get a 404 error when these php files are needed.
Edit
Here is my tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false
}
}
Here is my pacakage.json
"scripts": {
"start": "tsc && concurrently \"npm run tsc:w\" \"npm run lite\" ",
"lite": "lite-server",
"postinstall": "typings install",
"tsc": "tsc",
"tsc:w": "tsc -w",
"typings": "typings"
},

Trying to Migrate in Development Mode

I'm trying to figure out how I can get my environment set to development mode inside my Laravel application.
bootstrap/start.php
$env = $app->detectEnvironment(array(
'development' => array('blog.app'),
'production' => array('')
));
Inside my routes file I have the following:
Route::get('/', function()
{
dd(App::environment());
});
When I go to blog.app it says its set to production and I'm not sure why. The purpose of figuring out why its giving me production instead of development is because I am attempt to run the command "php artisan migrate" and its giving me the following.
My-iMac:blog my$ php artisan migrate
**************************************
* Application In Production! *
**************************************
Do you really wish to run this command?
So I need help understanding why its still saying application is in production mode when I'm attempting to be in development mode.
Go inside your project directory. Then navigate to bootstrap/start.php.
Edit the local array specified in $app->detectEnvironment with your machine name.
Laravel will automatically check you machine name every time and know that it is a local environment and use the local configuration files instead. To use the local config files you can navigate to app/config/local and edit the files there. The files in app/config belong to the production environment. You can copy these files in the local folder and then edit them accordingly, for example you database configuration probably will differ on your local machine and the server.
You can simply edit the files in the config folders and Laravel will detect automatically the environment according to the array in the start.php and use the config files related to the machine on which you are working. You can also specify more environments and create their entries in the start.php file and then go and create a folder by the same name in the app/config folder and create config files for that environment in that folder.
Example: If you want to have two local environments you can edit you start.php file like so
$env = $app->detectEnvironment(array(
'local' => array('Machine1', 'Machine2'),.
'local2' => array('Machine3', 'Machine4'),
));
And then navigate to the app/config folder and create two folders like local and local2 and have separate files for each of them. Now the machines with names Machine1 and Machine2 will automatically use config files present in local folder and Machine3 and Machine4 will use config files in local2 folder. This way Laravel provides great flexibility in setting environments.
For more information check out the Environments and Configuration Episode on Laracasts.
You need to figure out your machine name. In order to do that, run this code anywhere:
echo gethostname();
exit;
More info about the function: http://php.net/manual/en/function.gethostname.php
It will output your machine name, then you need to paste it in the 'development' array.

Is it possible to push a index.html to heroku?

I develop a pure front-end app with an index-file called index.html. Everytime I push to heroku, i need to rename it index.php, in order to tell heroku to use php. Is there another way to declare what type of server I want? So that I get to keep my file as an html-file?
You can also just drop an empty composer.json into your project to make Heroku treat your project as a PHP project.
The preference of index.php over index.html comes from the default DirectoryIndex directive that Apache uses on Heroku; it has nothing to do with your browser. To change it, you could drop a .htaccess with DirectoryIndex index.html into your application.
If you just want raw performance for your static site, it's also probably a good idea to use Nginx. The default configuration should be reasonable for your purposes so add a Procfile with web: vendor/bin/heroku-php-nginx to your project along with an empty composer.json (and no index.php) and you're good to go.
Also see https://devcenter.heroku.com/articles/php-support and https://devcenter.heroku.com/articles/custom-php-settings for more details.
You can do that simple node.js app with ExpressJS;
Let say your project folder has a sub folder called public,
var express = require('express');
var app = express();
app.use('/', express.static(__dirname + '/public'));
var port = Number(process.env.PORT || 5000);
app.listen(port, function() {
console.log('Your files will be served through this web server')
});
Save this code in app.js, put your static files under public folder, and prepare your Procfile
Final folder structure will be;
yourproject
--public/
----your staticfiles
--app.js
--Procfile
Procfile will be;
web: node app.js
And deploy your project. When you request your static files like; .../index.html, index.html will be served under public folder
Add a empty file called index.php, this will be used by heroku, though, index.html will be prioritised by browsers.

Categories