Where to put things in Laravel 5.1 - php

I'm a bit confused about where something like this belongs to Laravel.
I want to write a web service client wrapper in laravel, and I want to access it like this:
\MyWSClient::getSomeInfoAbout($someId);
then the code will connect to the web service to http://www.someapi.com/api/getSomeInfoAbout?id=$someid&type=json with OAuth2 or some token requests, then fetch the data, keep token information until it expires, refresh token if needed etc.
But where will I put the code? In the vendor directory as a new package? I'm moving this code from a computer to another computer except vendor,storage and node_modules folders, because they are huge and when I do this, I will have to move only one folder in the vendor directory. And I'll need to publish a package under development to composer if I want portability etc.
Is there any other way to do something like this?

I think I've found the answer.
First I needed to use jeroen-g/laravel-packager package to create a new package using artisan console. I could've done that by hand, but I didn't know the required files.
Second, I've created a new package in the packages folder with my desired class name.
Third, I've added the provider and the alias for the class in app.php.
After that, I've created a test method in my controller and wrote a route for that. I called the static method I've written in the SkeletonClass the packager created for me.
And it worked with some tweaking after creation.
I've used php artisan packager:new tpaksu mypackage --i command for an interactive package creation which is cool.
Note: I've just learned the existence of this package, I'm not advertising it :)

Related

About Google Calendar API and PHP

I am trying to write a webpage that will present to the public the contents of one of my Google Calendars. The standard iframe doesn't give me the format I'd like and I also want to embed some schema.org event information, update and submit a sitemap, etc.
As I'm fairly experienced with PHP, I'm trying to extract data from the Google Calendar API. I looked at Google's quickstart https://developers.google.com/calendar/quickstart/php but it assumes a level of knowledge I don't possess.
It seems I need to use something called "composer" and tells me I need to do "composer require google/apiclient:^2.0" but I'm unclear where exactly I type this - I can't see anywhere obvious in cPanel - or maybe it goes at the start of my PHP code?
Also, I note that the sample code is command-line only and I can't find code anywhere that would appear to be capable of running as a PHP web page. Is that because it's just not possible?
Composer is a dependency manager like bower, npm, yarn etc., the list goes on and on. Firstly you'll have to install composer here and add it to your PATH variable (method depends on OS).
Afterwards, when you create a directory for your project in the www-folder of your WAMP/LAMP or another folder acting as webroot, you can execute the composer require command. This will generate a folder called 'vendor' (with your dependency as a subdirectory) and a composer.json & composer.lock file, the latter 2 files will contain all the 'required dependencies' of composer.
You can then include the dependency using the require_once()-function in your PHP-script.
Hopefully this helps.
Cheers.

Laravel - share functionality between multiple separate codebases

So I have a project here that consists of several websites, and rather than writing functionality such as a login flow and signup flow into each project codebase I’m looking for a way to write these shared components once and include them as dependencies into all of the required codebases.
The login flow, for example, consists of:
model,
view
controller
repository
JavaScript
css
unit tests
database migrations
all other code related to the login flow
I would really like to be able to just include these shared functionalities via composer for example. Is there a way to do this?
Docker community edition, Laradock, Laravel 5.7, PHP 7.2, composer.
if you need to Share function you can make this function in helper function then put path in autoload path this link will help you for more understand

Moving default AppBundle under vendor

I have a new symfony project. By default it contains
-/AppBundle
-AppBundle.php
--/Controller
--/Default Controller
Since I am going to have more bundles I would like it to be under a VendorName called MyProject where I have my ApiBundle.
I have tried moving AppBundle manually, then changing namespaces in the files, yml files and AppKernel. But I still get an error
Expected to find class "AppBundle\AppBundle" in file "/Applications/MAMP/htdocs/healthy-living/src/HealthyLiving/AppBundle/AppBundle.php" while importing services from resource "../../src/HealthyLiving/AppBundle/*", but it was not found! Check the namespace prefix used with the resource.' in /Applications/MAMP/htdocs/healthy-living/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Loader/FileLoader.php:133
Is there any console commands for doing this, if not what should be the procedures of moving it.
Thanks in advance
There's no console command or procedure to do it because it's not what vendor folder was designed for. vendor folder is meant to store 3rd-party code so keeping your own bundles, which you are developing, in vendor is not a good idea.
Since I am going to have more bundles
There is no reason that you can't keep more than one bundle inside your src folder. In fact, when Symfony introduced Bundle system it was very common that src folder contained a lot of bundles.
(note that vendor folder is almost always added to .gitignore - that's because what I wrote before)
EDIT after clarifying what the question is about:
It looks like command to generate bundles has/had some issues with creating bundles without Vendor folder:
Issue
Pull request
I don't know which version of Symfony are you using but either way creating bundle manually is always a good idea and it solves your problem too. (you can create it without vendor name)
You can upload your API bundle to a repository (Github, Gitlab, Bitbucket, etc.) and then import it as an external dependency with Composer.
Since moving was too complicated because of the config files that has to be changed so I decided to do a workaround and remove it and then install via console under the same parent. Works like a charm. Althoug could be a method in cli for this.

Where to Place My Package in Lumen Framework

If I want to use some custom class in Lumen, where should I place them? The Laravel official document does not mention this in any of application structure, service container or package development. Actually I found the document quite confusing to some extents.
For example, I want to set up a service called Invitation, I know I need to register this class in InvitationServiceProvider but where should I place the Invitation.php which the actual class exists in? This package is used for some specific application thus I do not want to put it in composer packagist.
BTW, the version of Lumen Framework is 5.2.
So finally I created a folder named Service under app and just pull all the libraries inside...

Laravel: Where to Add booted and booting Callbacks?

The main Laravel application object has two methods, booting and booted. These methods allow you to configure callbacks. The application object will call these callbacks before and after it boots.
Where, as a Laravel application developer, can I hook into these events? Looking at the framework it seems like
bootstrap/start.php
is the obvious place — but if I put code here it'll be zapped in the next update. There's also
start/global.php
statt/{$env}.php
but these files are actually required in by a booted callback, which means the booting callback point would be unavailable.
Is there an intended place for me to hook into these events as a Laravel application developer? Or are booting and booted there for the core framework developers? Or has I made an incorrect assumption that I'm not even aware of? Laravel 4.2.6, but if there's context from additional versions I'd love to hear about it.
New to Laravel, not new to programming. Not asking for a specific task (yet), just getting a feel for what best practices/intended use is and is not.
As a formal answer, there's no defined place for this behaviour. But a note against your comments about start.php, this file will not be "zapped" when you update laravel.
As long as a file is not within the vendors folder, any laravel updates won't change your files. start.php is generated when you create a base project, but should not be changed with further updates.
composer create-project is responsible for creating your base "skeleton" application, and any composer update that you do will just modify files in the vendor folder. Just remember, do not modify files in the vendor folder and you will be fine!

Categories