Symfony3 using multiple AppKernel.php - php

I run with 3 sites with the same app on Symfony2, all of these sites have a custom AppKernel.php file. I'm now upgrading to Symfony3 and it seem that the composer.json including by default the file "app/AppKernel.php" and "app/AppCache.php". I would like to provide app/AppKernel.php for site A and apps/siteB/app/AppKernel.php for site B. How can i set a custom AppKernel location?

I see three easy solutions for you:
Remove the autoload entries for the AppKernel and AppCache class from the composer.json file.
Use different namespaces for each kernel class, change the autoload configuration to be able to load all of them and use the right namespace when creating and booting the kernel.
Similar to 2.: Do not add namespaces but use different class names for the different kernels.

Related

Bundle with another bundle as a dependency in Symfony

I would like to know if there's a way to create a reusable bundle that depends on a public bundle in my case, OneupFlysystemBundle ?
By adding OneupFlysystemBundle to my bundle's composer.json I can see that it's downloaded and present in the vendor folder.
I want to only include my own bundle in the AppKernel (which has a dependency on OneupFlysystemBundle)
Beside these solutions I ended using the non-bundle (library) version of OneupFlysystem as a dependency of my bundle and rewrite it as facade
Probably the best way is to use Symfony Flex. Which allows you to use recipes. An alternative would be to use symfony-bundle-dependencies

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 can you put a Service Provider Class in Laravel?

I want to create a server provider in Laravel.
I want this service provide to live under its own namespace.
Path\To\My\AwesomeServiceProvider
Where should I put this class? Normally I'd drop a custom class in
app/models
However, app/models isn't added as an autoload source until after app/start/global.php executes. This is too late for a service provider, as all service providers are registered in bootstrap/start.php.
Is there a way to create a service provider without placing the class in composer's vendor folder or monkeying with your composer.json classmap?
Put another way, is there a location where Laravel will autoload classes from prior to bootstrap/start.php being loaded that doesn't require additional composer configuration.
(For the inevitable "why don't you justs", the reason I want to avoid composer is I'm trying to figure out the bare minimum code and configuration needed for a service provider in Laravel)
you dont need to modify anything inside vendor.
You only need to define one of the possible autoload types for your new class or directory inside the composer.json of your project.
https://getcomposer.org/doc/04-schema.md#autoload
as alternate you can directly use a plain php implementation of the autoloading
https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md

Where do we put non-database functions/library in Laravel 4?

I'm a developer who is just migrating from CodeIgniter to Laravel. In CI I had library folder where I could put non database functions, for example email verification etc. In Laravel I couldn't find something like that ? Where can I put such libraries in Laravel 4?
If it is single PHP file containing different functions then you can create a Model class in your Model folder. But this Class will not extend Eloquent. It is a simple class. For example I have a class as General.php containing static methods of general functionality. As Model is autoloaded so I don't need to worry about including file. It is automatically included.
But if it is a directory containing different PHP files or PHP classes then you can create a directory in your app directory and add the path of that directory in providers array in app.php in config directory. You can also create an aliases of your library by putting entry in aliases under providers in app.php.
Create a library (just a folder), namespace it, and set it in config as a provider.
Then you can call it like
new \Foo\Class()

Where should I place my Monolog custom Handler in my Symfony2 project?

I have created a handler class that derives from AbstractProcessingHandler. I've seen that I can put it in src/MyNamespace/MyBundle/Monolog/, but it worries me a bit because this handler is used in several others bundles where I log data. So the other bundles will need MyBundle to work properly, only because of this handler.
I tried to put my handler class in lib/ but it does not seem to work (maybe I have to do something special with Autoload?).
Or should I create a new bundle specifically for this handler?
Edit: I can't really place my custom handler class in vendor/monolog/monolog/src/Monolog/Handler because then I would not be able to add it to my git repository: there is a conflict because this folder is managed by another git repository (created by Composer)
On Monolog's end there is really no restriction on where to put it or how you call it. The key is only that it implements monolog's HandlerInterface or extends from one of the existing handlers.
Now it depends what your handler is, if it's generic stuff that other people could use you could submit it as a pull request to monolog.
If not, you can either create an own composer package for it, or put it in src/Acme/Monolog/FooHandler or something like that, so it stays in your application but is clearly out of a bundle. The downside is that you need to configure it as a service in one of your bundles, so you still have some sort of dependency on a bundle there.
Maybe having it as its own bundle would make sense then. But it's quite a lot of boilerplate for just one class.
If all your bundles are application specific and very unlikely to be extracted out of it, having cross-bundles dependencies is fine though IMO.
The dependency is anyway not very strong since one bundle could contain the handler and configure it. The other bundles can still log to monolog, even if the handler isn't present, they can log. It just won't go to that specific handler. Nothing should break.
As you see, it's just a lot of trade-offs, and it's hard to say which solution is the most fitting without knowing more about your project.
If you want to have your handler class in lib/ you will need to add the lib/ folder to your composer.json autoload section. For example:
"autoload": {
"psr-0": { "": ["src/", "lib/"] }
}
Take a look at the Composer documentation:
Basic Usage
Autoload
I think the common approach here is to use a "Bridge" dir in your Bundle with a clear dependency. If you have other bundles that rely on this, what we've done is create a ServiceBundle which is basically for all shared services across all bundles within the application. This might not work well for you if you have plans of distributing this bundle, but may otherwise.

Categories