Is there any possibility how to call controller inside Blade template?
I’m rewriting my app from Symfony framework to Laravel, but Symfony's Twig allows me to show dynamic data in layout view by using embedded controllers, so I can keep things DRY.
Is there any alternative to embedded controllers in Laravel? Otherwise I'd have to set common data explicitly for every view.
You're looking for View Composers.
From the documentation:
View composers are callbacks or class methods that are called when a
view is rendered. If you have data that you want bound to a given view
each time that view is rendered throughout your application, a view
composer can organize that code into a single location. Therefore,
view composers may function like "view models" or "presenters".
http://laravel.com/docs/responses#view-composers
Related
I've just started using Slim and Twig and getting to grips with it all.
I am building a small site and will have a Twitter feed on every page. So, I want to put this in my layout template (base.html.twig). The only way I can see to do it is to pass it in to every route and then use an include that can access the details.
Seems like there must be a way to set it in the layout template once without passing it in through every route?
Any help or a related link would be great.
you can extend twig with user defined functions, just make a call inside template and you will not need for passing it every time http://twig.sensiolabs.org/doc/advanced.html#creating-an-extension
I have been working with Laravel for a while now.
However, I am faced with a new challenge. I have some notifications in my application which are going to be shown on the master template.
In this scenario, it'd be great being able to include NotificationController#getNotifications returning af view containing the notifications.
How can I achieve this?
I don't think you should include any controller to achieve this. Controllers are supposed to handle user requests. I think a composer is the way to go.
Create a composer for your master template that will provide the view with the notifications you want to be shown.
View::composer('layouts.master', function($view)
{
$notifications = Notification::all();
$view->with('notifications', $notifications);
});
Then render the notifications in the template file.
Another way to do this, is to add a composer to the notifications view and then include that view from the master template (using #include).
Note: Laravel doesn't provide a file or folder for composers to be in by default. I usually create a composers.php or a composers folder (for class based composers).
My question is how to make a hyperlink in a view? Like in HTML. Have I to create it in an action method in the controller and then send it to a view? or what exactly?
If you are using the Twig templating engine you would have something like
Home
If you are using PHP for your templates you would have something like
Home
homepage in the above examples is a route identifier that you must already have defined in your routing configuration
Define the anchor text in the view.
Views are exactly what they sound like they're for, providing view to the visitor. Controllers, models are all for the internal processing & logic of the application.
I'm developing a very small cms kind of thing. So far I have a page controller that lets you add pages. And there is a frontend controller. All calls are received by the frontend controller and respective page is displayed. Now I want to allow some custom tags while creating a page. I am using CKEditor and I want that while creating a page user gives a tag like <!--cmsform_printform--> and this tag will print printform method of cmsform controller.
For this I have created a controller cmsform that has a method printform. In my fronend controller I have created a method replace_tags that searches tags and then call respective controller and method.
But now I realized that my logic was false because I should not load a controller in another controller. I can't think of anyother logic. Please someone guide me or redirect me to a good codeigniter tutorial that explains this.
Thanks
Have you tried writing a helper instead? The functionality you describe sounds more like a helper method than a controller action.
CI manual in their site lists several excellent video tutorials, notably the ones published on NetTuts+. You might want to have a look, if you haven't already. Also, please do search CI forums. They contain plenty of information on pretty much any CI-related topic.
I'm trying to create a page that displays certain widgets/includes dependent on the users permissions. I'm quite new to CodeIgniter and MVC.
Example.
I have a calendar. This has a controller, model and view and it needs to be included on other pages.
I want this calendar and a number of other similar 'widgets' to be displayed on one page. This page currently has a controller and view.
I know I shouldn't be loading a controller from another controller, but can't see a way round it.
Can you load multiple controllers from a helper, or do I need some form of template?
Thanks,
Jacqui
It sounds to me like you are looking for Modular Extensions for CodeIgniter. This allows you to break an application into modules that can be called from one another. In your case, you would have a calendar module which you could call from your other modules.
What you might be looking for is something called Layouts. Codeigniter does not have such a feature out of the box but layouts are basically delegating a (front-controller) request across multiple actions. Each action works on it's own and has it's own view assigned.
The layout will take care to put this all together into one output then.
This methodology takes care as well that you don't call one controller from another controller so you keep code apart that does not belong to each other or would get messed up because it does too much at once.
This discussion might be interesting for you: Codeigniter layouts?, the Codeigniter FAQ has more about it as well and more specifically Header and footer and menu on every page in the wiki.