I am new in laravel and I have good experience in Cakephp. In Cakephp we use helper for views only But I have seen my exiting code in laravel, We use helpers in controller also. Is it fine to use helper in controller or not?
If yes then Please let me know, What is the disadvantage or advantage of using helpers in controllers?
Please suggest me...
If you're talking about global Laravel helpers, then yes, you can use them everywhere in your app (controllers, models, view, middleware, routes.php etc), because these are global helpers.
You can also define your own helpers which will also be available globally.
Related
I find myself writing same code for my resourceful controllers. Is there is a way to extract the the functions index(), create(), update(), show(), edit() & destroy() into a generic file ? Also, if we do that, will it give me an option to override the those functions in the respective controller?
All you need is a crud generator package like nvd crud generator. It creates working templates for controller, model and views which you can edit yourself to suite your needs. This solution will save you the trouble of writing the same code again and again with the flexibilty of customizing each controller.
However, if you need to have a generaic controller which you can extend from, you can do that as well. You can create a controller ResourceController with artisan make command and then extend all other resource controllers from it.
I've been using Laravel for a long time, and I'm now writing a micro-project using Lumen.
I need to pass some variables to all views. In Laravel I can use the View::share() function in a middleware or in the controller's constructor, but in Lumen there is no View class, and it looks like all view functionality is simply View::make() alias.
Is there a way to share variables to all views?
For performance reasons, Lumen doesn't register facades and service providers the way Laravel does. While the Laravel facades are included with Lumen, only some are aliased (View not being one of them), and only if you uncomment the $app->withFacedes(); line in bootstrap/app.php (you can check the Laravel\Lumen\Application::withFacades method to see which ones). So in order to use other facades such as View, you either need to alias the facade class yourself:
// "bootstrap/app.php" is a good place to add this
class_alias('Illuminate\Support\Facades\View', 'View');
Or you can include it with use wherever needed:
use Illuminate\Support\Facades\View;
The correct way to share data with views in Lumen is:
app('view')->share(...);
Some of Laravel's functionality that is not explicitly described in Lumen documentation can be accessed with Lumen's app() helper function.
I'm using CodeIgniter and I have a few utils that I want each controller (that is, each controller file) to have access to.
The question is: where to put these?
I thought of helpers, but the CI documentation talks only about extending existing helpers, not making your own. I'm sure that doesn't mean I can't make my own helpers, but it does mean I don't know how they should be built (procedural? Methods of the global CI instance? etc)
I also considered hooks, but this is a poor fit I think as I'm not extending core functionality.
Or is there some other way I'm missing?
It's been a while since I've done this but I believe I used two approaches.
Creating a new, custom helper that goes into /application/helpers, following steps noted from this answer: CodeIgniter: Create new helper?
Creating a new library class into /application/libraries which I also activate in the autoload configuration found in /applications/config/autoload.php. This way it's always available to my controllers when I need it. CI has good documentation on this one (http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html).
I did it simply by adding a file to the application/helpers folder (maybe I created that folder - I can't remember) and then loading them in the usual way.
I'm using Laravel web framework for a PHP project. I have a question about this framework, the documentation (which is poor), and the community from IRC doesn't helped me.
I want to create a controller which will be executed before any other controller. I want in this controller to pass some variables to view. How is this possible?
Thank you.
You can do such thing in the before filter, in routes.php (, for Laravel 4 its filters.php).
Or if you need to do it in controller then create a BaseController which should extend Controller, in Laravel 3 you should have this already, base.php. And all your controllers should extend this BaseController, in its __construct or in before filter do this for ease:
View::share('key', 'value');
And use it as $key in your views.
Note, View::share can be problematic sometimes.
How do I use a component that I created in cakePHP inside one of my model classes? Is this possible?
If so, please let me know how I can do so
It is possible but pretty bad practice in a MVC framework. You should re-think and re-organize your code if you think you need to use the component in a model because something is cleary wrong then.
A component is thought to share code between controllers, only between controllers.
Components in CakePHP 1.3
Components in CakePHP 2.x
Components in CakePHP 3.x
To share re-usable code between models it would be a behavior. For a view it would be a helper.
If you have some really generic code it should be a lib or put it in the Utility folder / namespace or create a new namespace. Check the existing classes there to get an idea what to put in there.
No code was provided so it is not possible to give any real recommendation on how to refactor it. However the way you want to use the existing code won't work in the MVC context, so time to rethink your approach of whatever you try to do.
It is possible to use a component inside a model (but I cannot comment if this is a recommended or a best-practice).
Inspired from original source, an example to use a component called ‘Geocoder’ in a model:
App::import('Component','GeoCoder');
$gc = new GeoCoderComponent(new ComponentCollection);
Then you can use $gc to call the functions of the component.
--
P.S.: I do not want to encourage bad programming practices, but sometimes the pressure of deadlines (in real world projects) may force a developer to make such decisions.
#AD7six
// Use anywhere
AuthComponent::user('id')
// From inside a controller
$this->Auth->user('id');
From the cake PHP documentation they provide AuthComponent::user('id') so that it can be used in places other than a controller.
Maybe I need a bigger hint, but why shouldn't my model be able to access ACL information ?