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()
Related
I have PHP example on how to use Yelp Fusion API. It uses OAuth.php file with several classes. In main example it is imported with
require_once('lib/OAuth.php');
Can I do the same in Laravel?
Or I'd better provide namespace for OAuth.php file and put it somewhere on the tree? Where to put it?
I suggest your to make a new directory inside app and call it like "Classes" and store your OAuth.php as "/app/Classes/OAuth.php".
Don't forget to put a namespace App\Classes; to top of this file.
Due to having several classes inside your file I suggest to rewrite this a bit and separate each class to file
You can use the Yelp Fusion API with any OAuth implementation. To simplify the process and maintain a clean project, you can use an OAuth package.
You can find a list of OAuth packages for composer here.
Using packages instead of custom PHP files would help to keep project clean.
If you do need to add any custom PHP files, it's recommended to create a directory within the /app directory for organization. A good directory name could be "Libraries" or "Helpers," depending on the type of content being stored.
Note that directories and files within the /app directory are automatically loaded by Composer.
I have a ZF2 model under a module and I want to call functions in this model from a plain PHP file. This file is located outside of the project folder.
Is there any way?
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
Is there a general place that I could put logic that can be shared between the controllers and commands in Laravel. I have functionality that will most often times be run from command line via stored procedure, but also need the same (or a subset) of the functionality via web.
Can I use controller logic within the command? Or call the command from the controller/route?
Or should I just build my own classes and include them as needed in both?
just create a app/libraries folder. In that create a custom_helpers.php file and autoload it. Store your methods that your going to be using often there. Think of it like the helper class provided by laravel by default.
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.