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.
Related
I created a custom Symfony command to manage entities in my DB but I don't know how to use doctrine in it (e.g. import it like in a controller).
Thanks!
Using a ContainerAwareCommand is fine.
$this->getContainer()->get('doctrine');
Using the container directly is not a good practice. Sometimes you will need to modify class code and when that time comes you will need to review the whole code to find any dependencies.
Register the command as a serivce and pass serivces that are actually will be used in the logic.
Check How to Define Commands as Services
I have two apps running at same server and one of them is CI. I need the one that is not CI to use models, libraries, helpers etc. So I needed something like get_instance(), but I'm not in a controller's context, I'm outside CodeIgniter.
How can I get a generic instance of a controller, or instantiate models or import helpers from a different application?
Example:
A and CI are apps and both runs on the same server
ROOT
A
script.php
CI (it's a CodeIgniter project, with controllers, models etc)
...
random_model.php
...
random_helper.php
How can I make script.php on A use random_model.php on CI?
Nice question, but I'm afraid that you can't do that, and even if we assume you can do it, this will do more damage than good, for example you call a custom library that is using the session library, then your application "A" will implicitly use that session library and can have unpredictable consequences,mostly if "A" is using native php session.
This is not only true for session but other configuration settings too (database, base_url,charset,...and other settings) and it will auto load other libraries, helpers, packages ...etc if any from your autoload.php
a better way is to create a controller under the "codeigniter" project and call it as you usually do with other controllers.
or even better, create a second codeigniter project, since it has a very small footprint, for your script "A" and avoid all conflict between the two projects and get more organized
I hope that makes sense.
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.
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.