I need to add service layer in my project where controllers are doing all work now.
Can I use PHP artisan to make service files, as we do for controllers: php artisan make:controller ?
How do you add service files?
I'll leave an answer just in case someone is also looking for it.
No, there is not such thing as service layer out of the box in Laravel, you should either implement it yourself or you can require a repository which implements it https://github.com/TakeoO/laravel-service-layer
Related
I'm starting out learning Laravel, and all the tutorials show how to use
php artisan make:controller BlahController
to make a new controller.
Is there a requirement I do this from the command line? If I want to do it by hand, what would I need to do to replicate that action?
I know that of course, I'd need to manually create the BlahController.php file inside the app\http\Controllers folder.
But does artisan make:controller alter/create any other files?
Broader question. Is php artisan required at all to work in Laravel? Or can all development be done by hand coding?
No, it is not required at all to use php artisan to create any files.
The purpose of artisan is to make your work more automated or less coding in general, but you are 100% correct, you can just manually create the controller file on its own (or model or view or anything related to laravel) without using artisan.
Example of artisan is to do things like:
php artisan create:model MyModel -all
This will create:
migration
seeder
factory
policy
resource controller
form request classes for the model
all of that is created and ready to be edited in a single command rather than having to manually create each file and filling it up and checking if the names are correct etc...
You can do so many things with artisan to simplify the process, and you can always read more on what options you have when creating things by doing:
php artisan create:controller -help
More of the documentation can be found here: https://laravel.com/docs/9.x/artisan#introduction
Laravel artisan helps you to do things faster
Ofcourse you can create it manually but artisan makes your life a little bit easier!
I highly recommend it
Also It's not about just creating a controller
It does a lot, read more about it here:
https://laravel.com/docs/9.x/artisan
I wish to add chargebee php api to my laravel project
For that I first ran
composer require chargebee/chargebee-php
This added the chargebee API to vendor/chargbee
Next thing I would need to do is to include and configure the api. This can be done by:
require_once base_path('vendor/chargebee/ChargeBee.php');
ChargeBee_Environment::configure("your_site", "{your_site_api_key}");
My question is where should i do this? I would like the including of ChargBee.php and configuring it to be in a generic place so i can later use it in any Controller and it would already be set up. What is the default way to do this in Laravel?
You should set up a new service provider, like described in the official documentation.
Hint 1: Run php artisan make:provider ChargeBeeServiceProvider from command line to generate the new service provider.
Hint 2: take a closer look at the register() method. You should register the ChargeBee "service" there, most probably like a singleton. That's the place, where the API configuration should be put.
Since autoload is being used in the chargebee library (see its composer.json), there is no need for the require_once base_path('vendor/chargebee/ChargeBee.php'); line. Just simply use the classes.
I've seen this How can i inject dependencies to Symfony Console commands? but that answer doesn't really give enough information and is already explained here http://symfony.com/doc/current/cookbook/console/console_command.html
The problem is a containerAwareCommand doesn't work with the setup here http://symfony.com/doc/current/components/console/introduction.html
In order to use containerAwareCommand from what I can tell, I need my application to use
Symfony\Bundle\FrameworkBundle\Console\Application
instead of
Symfony\Component\Console\Application
But using the frameworkBundle Application class requires an instance of KernelInterface and won't allow me to pass in a name and version to my application.
Here is what I have that won't work with containerAwareCommands
#!/usr/bin/env php
<?php
require __DIR__.'/../src/vendor/autoload.php';
$app = new Symfony\Component\Console\Application('spud', '0.0.1');
$app->add(new Isimmons\Spudster\Console\Commands\SayHelloCommand);
$app->run();
The command it's self runs but I get an error when trying to use getContainer
Call to undefined method Symfony\Component\Console\Application::getKernel()
On a related topic which will probably come up next, The documentation for registering a class in the container shows using a app/config/config.php file. But I don't have an app directory since this is not a full symfony application. My base directory in which all of the app except for the file above is located, is src/lib. If I can figure out the first part above, will symfony be able to find the config file at src/lib/config/config.php?
You can use Consolefull application.
Consolefull is a simple library to work with Symfony Console Component and Symfony Dependency Injection Component
I'm porting an old project to symfony2 in order to start learning the framework and I cannot realize how to properly add some helper functions I have in a PHP file.
Services. Wrap those function in a class (like Helper), define the class as a service and then inject it where needed (controller or another service, or even into a cli command).
The namespace (thus the path, as you tagged the question with psr-0) is up to you.
I am a newbie in Symfony2 and I can't understand where I should make includes with my custom cross-projects functions (e.g. array_merge_overwrite, array_last, etc.)? I use both types of apps: web (MVC) and console (extends ContainerAwareCommand).
Or there is another "right way" for this?
Create a service and put your common functionality in it. For example, you can name it ArrayService and register it in the container as array.service. You can then access this service from controllers via
$this->get('array.service');
and from commands via
$this->getContainer()->get('array.service');
So, your code will look something like this:
$element = $this->get('array.service')->last($array); // or ->arrayLast($array)
If you need the same functionality across several projects, make a bundle with that service and add it to the deps file of each project. Then it will be installed when you run the bin/vendors install script.
You can convert your functions to static methods of some class to make them autoloadable. Or... well... Place them where you want and require() from where you need them every time.