How to make controller accessible from every controller?

For programming and general questions on Zend Framework
Post Reply
a1a1
Posts: 20
Joined: Mon Apr 13, 2009 5:14 pm

How to make controller accessible from every controller?

Post by a1a1 » Mon May 11, 2009 9:55 pm

Hello,
I<ve been following a tutorial on Zend_Acl and Zend_Auth to create a login system. I managed to setup a login page that worked.
What I would like to do now is add this same form in the main layout.

As it stands, the login works with a login controller file coupled with a template view file which simply calls up the form like so:

Code: Select all

<div>
<?php echo loginForm(); ?>
<div>
So I transfered this simple statement over to my my layout file under the layouts folder in the classic Zend Framework structure.

The problem is that the loginForm function, along with a few others necessairy to make it work, were declared in the loginController page.

Besides the unnatractive solution of adding these functions in every controller of the pages that should include the login form, where should I place these functions and could I call them from the bootstrap?

Thanks in advance for your time

a1a1
Posts: 20
Joined: Mon Apr 13, 2009 5:14 pm

Re: How to make controller accessible from every controller?

Post by a1a1 » Tue May 12, 2009 2:15 pm

I think the way to do this is to extend another controller. Going to give it a go...

rampog
Posts: 56
Joined: Mon May 04, 2009 12:15 pm

Re: How to make controller accessible from every controller?

Post by rampog » Tue May 19, 2009 12:17 am

yups... create baseController and extend it ....

brunitto
Posts: 111
Joined: Fri May 29, 2009 10:10 pm
Location: São Paulo - Brazil
Contact:

Re: How to make controller accessible from every controller?

Post by brunitto » Sun May 31, 2009 4:43 am

Maybe it is an approach problem. Why had you moved the login form to the layout? There is a good reason to render the login form in every view? I think it would be simpler if you move all login responsabilities to a login controller and models - if necessary. Keeps your controllers thin and move application-wide responsabilities to models.

Although all login workflow is within login controller, you can use a plugin or action helpers in each controller that needs authentication - or in a base controller class if all controllers must be authenticated.

Try to keep it simple and maybe you will solve this issue by yourself.

Good luck!
Ookie dookie!

a1a1
Posts: 20
Joined: Mon Apr 13, 2009 5:14 pm

Re: How to make controller accessible from every controller?

Post by a1a1 » Mon Jun 01, 2009 2:40 pm

Hi, thanks for the advice... I ended up extending the controller and the site has been launched. This was my first ZF project so I alsready know what to avoid and what I'll be doing differently for the next project. What made this difficult was that I was trying to incorporate scripts from tutorials when almost all of them used a different underlying directory struture. In the case of my login box, I needed it as part of the site-wide header, as many websites do. I now realize it should have been built as a model rather than a controller. I just couldn't get it to work as a model initialy. The learning curve is steep and long but I learn best through experience, trial and error and real world examples rather than abstracts.

brunitto
Posts: 111
Joined: Fri May 29, 2009 10:10 pm
Location: São Paulo - Brazil
Contact:

Re: How to make controller accessible from every controller?

Post by brunitto » Mon Jun 01, 2009 2:50 pm

Then you had a good reason to keep the login form in every page... lol

I must agree that the 'M' from MVC is a little bit exoteric in the start but the things are getting better with 1.8+.

Keep trying and good luck!
Ookie dookie!

fast8753
Posts: 1
Joined: Sat Apr 21, 2018 9:41 pm
Location: Canada
Contact:

Re: How to make controller accessible from every controller?

Post by fast8753 » Sat Apr 21, 2018 9:50 pm

Nice

Post Reply