How to use Git With Laravel? - php

I'm pretty new to Git. I'm developing using PHP/Laravel on at least two machines; both Windows 10, let's call them office and home. I want to keep a sync environment on both. So I created an account on BitBucket.
I created my Laravel app using Laragon using the directory: d:\laragon\www\project
I created a clean remote repo in BitBucket and configured for use on the office PC, inside the project directory:
git init
git remote add origin https://...
git add .
git commit master
git push -u origin master
It copies some of the files to the remote repository. I understand this is because of the .gitignore file, and that's okay.
Now the thing is if I go to my home PC and do a:
git clone http://...
It will only get the non-ignored files. My question is, how do I have to configure the second development environment?
I tried to create a new app at the home's c:\laragon\www\project and then try to clone in this directory, but of course it says the directory is not empty, so does nothing.
Do I have to delete the .gitignore file the first time, so it syncs everything?

I'm assuming that you already have your second machine with the basic set up (php, composer, laravel, local server, node and so on..)
First of all you need to install your composer dependencies (listed in composer.json), to accomplish this run:
composer install
The .gitignore will only ignore.. well.. the desired ignored files, such as: node_modules and .env for example. To solve this, install your dependencies (listed in your package.json, that is not ignored by default) in your second machine using npm or yarn:
npm install
// or
yarn install
In the case of your .env file, I suggest you to clone the .env.example (creating the desired .env) and set your keys in every machine, because any of them can have a different setup.
Of course, your Laravel encryption key needs to be generated, so run:
php artisan key:generate
Finally, migrate your database (and populate it, in case you set up seeders) like this:
php artisan migrate --seed
// notice the '--seed' flag is used when you want to run your seeders

Related

Update project created with composer create-project

There are some frameworks like laravel which recommend installation using create-project.
It would be hard to update projects like this through composer, because
The composer create-project creates the skeleton with all initial
routes in your configuration etc.
From the very very first moment you are starting to change the default
routes, removing the default controllers and changing the default
views, your project would be out of sync. because meanwhile laravel
changes the skeleton to newer versions with some new default routes
etc or event changes directory structure.
However, I've recently seen that phpmyadmin recommends composer create-project as a possible installation method.
As phpmyadmin does not simply provide some skeleton files to be modified by the user, but a complete, finished web-app, I'd like to know what's the best way to update a phpmyadmin installation created like that?
I don't know whether there is an official way to do this.
According to the docs, create-project is the equivalent of:
doing a git clone/svn checkout followed by a composer install of the vendors.
If you haven't modified any of the files, I think the easiest way would be to just delete the directory and run composer create-project again.
If you have modified some of the files, you could do a git merge (if the project uses git) and run composer update again.
If you haven't yet created the project, you could run create-project with the --keep-vcs flag and then every time you want to update it, you can cd to the project and run:
git pull origin <version>
composer update
If you have already installed the project without --keep-vcs, then you'll have to make the directory a git repository and then add the project's repository as remote. To find the project's repository, search for it on Packagist. For example, for phpmyadmin:
cd phpmyadmin
git init
git add .
git commit -m "Add initial files"
git remote add git#github.com:phpmyadmin/composer.git
git pull origin <version> --allow-unrelated-histories -Xtheirs
composer update
Some of your changes might be lost with the above git pull though so make a backup (and maybe research other ways to merge unrelated histories).

setting up existing laravel project in Ubuntu

How to setup existing laravel 5 project and there database in Ubuntu 14.04 using xampp?
Some of things you need to consider for new setup. If you have setup git repo then
You can just pull from git If not copy/paste all folders except vendor, node_modules.
Use composer install to make vendor folder
Use npm install to make node_modules folder if you already using
Use php artisan migrate to make database structure
Use php artisan db:seed if you set any seeding to fill tables data
Use php artisan key:generate to make security.
Create/Update .env file to make work configuration from environment
Now you have to point your domain with root folder of laravel you created here.

How to install composer for portable

I have a repository with a bunch of code in it and a composer.json and a composer.lock.
When I want to run the website on a different server I have so clone the repo and then run composer install in the root of the repo.
However I want to be able to download the repo put it on a server and it should just work, without having to execute anything.
Even if I included the vendor folder in the repo and then try to put that on a webserver it gives me errors like "bla bla require() failed in some php file in vendor folder".
So what am I doing wrong?
A deployment of an application using Composer usually goes like this:
Checkout the appropriate version of the application from Git. You could also run git archive and pipe the result into tar to create a version that does not have the .git folder.
Run composer install
Transfer the created folder structure onto the target system.
All in all you'd probably only copy everything you already have on your development machine to the new system.
If that does not work, you have to be more specific with your error message.

Laravel 4: Running composer when deploying to Heroku

I've been searching everywhere for an answer to this.
I built an app in Laravel - I chose Heroku for deployment, I included a custom Procfile and httpd.conf to rewrite the document root to public. That all works great.
However my app shows an internal 500 error upon loading because it hasn't loaded any dependencies i.e. not run composer upon deployment.
My question is: how do I make composer run upon deployment?
p.s. I'm a total noob with this kind of stuff so please provide as much detail as possible.
The only possible solution I've seen is here: http://bergie.iki.fi/blog/using_composer_to_manage_dependencies_in_heroku_php_apps/
But I'm convinced there is better way than this.
Try the following
Composer Fix
You need to generate and include composer.lock in the repo, but Laravel's default .gitignore ignores composer.lock
Remove composer.lock from .gitignore
$ composer install
Add your Procfile
Heroku knows which processes to run for your app based on a configuration file called a Procfile. The default apache2 process (if you don't use a Procfile) points to the web root, not to /public, so we need to create a custom Procfile to serve the site from /public.
Add a file with the name Procfile that contains this line:
web: vendor/bin/heroku-php-apache2 public
Deploy your code to the Heroku app
$ git push heroku master
Another possible cause is that Heroku doesn't recognize your app as a PHP app, for example because of the presence of package.json in the root. And so it doesn't do composer install for you.
Solution is to specify a buildpack, and do a push again.
heroku config:add BUILDPACK_URL=https://github.com/heroku/heroku-buildpack-php
See also:
Heroku doesn't recognize my Laravel app as PHP app, and does not do composer install

Repository deployment and Composer : what workflow?

As a PHP developer I find myself working with Composer a lot. In the past it was on personal projects and such so I didn't have much problems with it, but now with Laravel 4 it's on project that require deploying and I'm in kind of a struggle to adapt my workflow.
All my projects are git repositories, thus per convention and because it's still quite buggy, like most developers I put the vendor directory in my .gitignore.
Now the problem is : I also use Git to deploy to the server, and by all logic the vendor directory is not uploaded as it's not tracked by the repository.
So my question is towards people that have worked with Composer and Git for longer than me : what is the best workflow to keep the server in sync ? How to track the vendor folder without really tracking it ?
I tried uploading it every time I update with Composer but some of my vendor folders are quite big and I can't manually upload 30Mb of files every time something updates.
I don't really know, how do you guys work it out ? I tried not ignoring the vendor folder but Git just messes it up, half are recognized as cloned repos and are just ignored anyway, etc.
UPDATE : Note that I'm on a shared host so I don't have access to the server's terminal.
The best way is to run composer install on the server after updating to the latest code. You should also make sure you commit your composer.lock file, which the server will then use to install (you should not run composer update on the server).
Capistrano (or Capifony if you are using Symfony2) is very useful for deployments with composer. You can trigger a deployment remotely and it will run a composer install in isolation so the site remains online until it has been deployed successfully. There are many other benefits such as retaining previous deployments and rolling back, copying old vendors before deployments, compiling assets etc. etc.
I'm working on something like this in the git post-receive hook on the server. This isn't tested and may be buggy, but you should get the idea.
#!/bin/bash
# get the updated composer.json
git checkout master -- composer.json
# only do this stuff if composer.json is different
# you could check this manually, or with git or cmp
cp composer.json tmp/composer.json
# may take a minute, but won't take the site down
(cd tmp; composer install --prefer-dist)
# this doesn't seem to be atomic
git checkout -f
# switch vendors over
# this isn't quite an atomic operation, but is very close
# you could probably do it with symlinks and "mv -Tf" to make it atomic
mv vendor vendor.old
mv tmp/vendor vendor
rm -r tmp vendor.old
Ideally all of the deploy (i.e. in this case the git checkout and the composer install) except one single mv would happen in isolation, outside of www. This doesn't work if you have untracked files (eg CMS uploads) in your working tree and rely on PHP's __FILE__ not resolving symlinks (due to this PHP bug).
This is an old question but in case anybody is looking a solution:
I slightly modify the #dave1010 answer to use git pull instead of git checkout--force
#!/bin/bash
# get only composer files
git fetch
git checkout origin/master -- composer.json
git checkout origin/master -- composer.lock
# make sure tmp is empty
rm -rf tmp
mkdir tmp
# copy the composer files to tmp
cp -r vendor tmp/vendor
cp composer.json tmp/composer.json
cp composer.lock tmp/composer.lock
# may take a minute, but won't take the site down
(cd tmp; composer install --no-scripts --verbose; cd ..)
# switch vendors over
rm -rf vendor_old
mv vendor vendor_old
mv tmp/vendor vendor
# update your code
git pull
# run again composer install. This time will print 'Nothing to install or update'
# but will execute pre/post scripts & generate autoload files
composer install --optimize-autoloader
There is maybe a better solution using capistrano/composer. But I like mine better.
You can use something like jenkins to ftp your files over this way you can direct jenkins to run composer install on the jenkins server and then ftp the files over.
This also allows you to ignore the vendor folder.
It does require a build server to be made and you would need to be able to execute commands vs the build server
The key is your composer.lock file. The composer.lock keeps track of exactly what packages (and versions) you have installed. When you deploy, send your composer.lock file up to the production server as well, and simply do a composer update. All the exact same package versions will be installed. With deployment software like Capistrano or Flightplan you can make the composer update step part of the process so it happens automatically.

Categories