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.
Related
I'm finally making the switch to OOP from procedural and starting with CodeIgniter and just looking for clarification. For every page that you would typically have (about.php) will you have both a view (about.php) and a controller (About.php) for every page? I'm trying to wrap my head around it and I've always given up and went back to procedural and now I'm forcing myself to learn object oriented.
That's certainly one way you could do it, but it doesn't have to be that way.
In CodeIgniter, your controller forms a section of your site, and looks in most URLs like a sub-folder, e.g.
http://example.net/products/tvs
There, products is the name of a controller, and tvs - in URL terms, the sub-page - is a method of that controller.
So yes, you could have a controller for each page, but no, you normally wouldn't. Think instead of controllers being responsible for site sections rather than individual pages.
The controller controls everything, you could have all your info in a DB, but if you want to have actual files, then put them in your "view" folder and just add a function in your controller.
Something like this:
controller:
function about_us_page() {// load your about_us.php view file here}
view:
about_us.php
and you should link to it like:
example.com/controller/about_us.php
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
I'm building a website using CodeIgnitier.
My problem is how can I use the MVC pattern inside a webpage.
Let's say I have a controller called "Settings", Within this page I want to divide the settings into categories, with tabs, like "System", "User" and so on.
I know how to do this in the usual way, something like "settings?tab=system" and then load the proper view according to the tab selected. But I want to make a separate MVC pattern for this settings page, is it possible? and how?
Basically the thing you are looking for is can be controlled using routes.ini or Route.php. The tab can be your querystring. Settings can be a controller and u can have an action called index which can be controlling it.
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.