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
Related
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.
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...
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.
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()
I am trying to create an alternative view and found this answer:
How to load view from alternative directory in Laravel 4
which suggested using this code
View::addLocation(app('path').'/themes/default');
View::addNamespace('theme', app('path').'/themes/default');
But cannot decide where to declare these statements . In which file can I use this code?
start.php,path.php,app.php,global.php or in another file .
If using the app/config/view.php configuration file to add view loading locations (via the paths array) is not enough for your needs, you can probably fit that into a service provider.
Laravel actually uses the View Library's Service Provider to register the view paths locations (based on the app/config/view.php config file as mentioned).
One thing you can do is add your own service provider class and add in your view logic there, in order to add a location / namespaces as you need. (You can even have your service provider read your own configuration files in order to determine locations/namespaces).
If you need help creating a service provider/don't know where to put one, read this on creating a Laravel application library.
If that's all you'll be doing, putting it inside app/start/global.php works just fine. There's really no need for a new service provider for such a simple task.
However, if after some time you realize your global.php file is starting to get too heavy and messy, then you should go for a service provider, as #fideloper mentioned.