Creating my own Git repository from a laravel project
I'm new to laravel, github and the whole packages deal, and i'm liking it. I'm afraid i stood still for to long and i've missed a train. So i'm trying to catch up on that.
I'm creating my first Laravel project and i've already pulled in a few packages from github but now i have this API i want to use, but there are no packages available on github. Therefore i want to create my own package and eventually publish it on github. Now i'm a bit stuck on how i should set this up in phpstorm and what principle i should follow.
I'm going to refer to MyRepo as the package i want to publish on github which will mainly be a collection of classes with a Laravel facace that makes calls to an API. It will also rely on other packages like GuzzleHttp
Questions i'm asking myself right now and can't seem to find concrete information about:
- Should i create MyRepo inside my Laravel project or should i create a separate phpstorm project for this?
- How should i setup phpstorm for this? I already setup Git VCS but depending on question 1 i need to do it differently
Please sign in to leave a comment.
Hello,
Separate project will do better in terms of isolation and is advised.
Just do VCS > Import into Version Control > Share Project on GitHub and it will do the rest, you just need to enter the repo name and description.
Okay, separation is indeed better for isolation, but then I wonder how I can run the code when it's outside the Laravel project because it relies on Laravel classes, methods, helpers, ...
>but then I wonder how I can run the code when it's outside the Laravel project because it relies on Laravel classes, methods, helpers, ...
You put required packages as a dependencies in your composer.json "required" section -- just like any other Laravel-based package does.
Have a look how barryvdh/laravel-debugbar, barryvdh/laravel-ide-helper, recca0120/laravel-tracy, mccool/laravel-auto-presenter .. or even better -- laravel/socialite itself does it (last one was part of Laravel framework itself at some point in the past I believe) -- have a look at their composer.json.
That i know, but that's only for a complete working package that's published on github, right? But before that, while i'm still developing the package I will need to run/debug my code somehow. I guess that's only possible when the package is installed in laravel, but not through composer.
I know how to manually register a package in Laravel using "autoload/psr-4", but is that the way to go? And in this case the package is inside a laravel project, how can i tell PHPStorm then that a certain subdirectory should be a standalone package that can be published to github?
Oh now it hit me that i've already read something about this, it's actually also called packages in Laravel: https://laravel.com/docs/5.5/packages
Come to think about it i remembered there's even a package that simplifies this by providing a boilerplate for a new package: https://github.com/Jeroen-G/laravel-packager
But the question in my previous post remains.
Apparently new packages are stored in "packages/MyVendor/MyPackage". So from my Laravel PhpStorm project, can i configure the directory "packages/MyVendor/MyPackage" is a github package and maybe "packages/MyVendor/MyOtherPackage" is another package or should i create a new PhpStorm project for each of these directories?
Well ... while developing .. it can be placed anywhere in your project and do not have any composer.json -- let it be just part of your project. Once you are happy with it (you feel it's time to have v1 that you are not afraid to show to the public) -- you can separate it into an standalone project and then publish it on GitHub (if not yet) and packagist (so it can be easily installed via composer by others).
>But before that, while i'm still developing the package I will need to run/debug my code somehow. I guess that's only possible when the package is installed in laravel, but not through composer.
No. The code is already local (it can be run locally) so it can be debugged.
What you can do though .. is instead of telling Composer to download it from GitHub/etc (as it would normally do if published on packagist) .. you can configure Composer so that particular package will be taken directly from your computer, from another folder ( I don't remember exact recipe but it should be in documentation as I have seen quite a few mentions in different places).
You can also use a symlink so that your actual Laravel project has always the latest version of your package (so no need to run "composer update" in any way and everything else that will be required for that).
Since you are planning to publish it as separate package/library .. I believe it will be better in general to develop it in a separate project: this way you will be more strict in what you can do and how so it will be more independent as if it will not have any access to your actual main project (so any errors that you may accidentally introduce may be easier to spot etc).