Change the layout from module bootstrap

7 messages Options
Embed this post
Permalink
Simeon Goranov-3

Change the layout from module bootstrap

Reply Threaded More More options
Print post
Permalink
Hello,
I've got a module bootstrap file Admin_Bootstrap where the module is
"admin". I'm wondering how to change the default layout, which in my
case is layout.phtml only for the admin module. In my opinion I have to
do it into the bootstrap file, because in this way the change will
affect all of the module controllers. Do you have any idea how to solve
the problem ?

Regards,
S.G.


Jurian Sluiman

Re: Change the layout from module bootstrap

Reply Threaded More More options
Print post
Permalink
Some javascript/style in this post has been disabled (why?)
Place an init method inside the class like this or something:


>public function _initLayout ()
>{
> Zend_Layout::getMvcInstance()->setLayout($myLayoutName)
>}


Regards, Jurian
--
Jurian Sluiman
Soflomo.com


Op Tuesday 03 November 2009 17:25:17 schreef Simeon Goranov:
> Hello,
> I've got a module bootstrap file Admin_Bootstrap where the module is
> "admin". I'm wondering how to change the default layout, which in my
> case is layout.phtml only for the admin module. In my opinion I have to
> do it into the bootstrap file, because in this way the change will
> affect all of the module controllers. Do you have any idea how to solve
> the problem ?
>
> Regards,
> S.G.



signature.asc (204 bytes) Download Attachment
vRandom

RE: Change the layout from module bootstrap

Reply Threaded More More options
Print post
Permalink

You can also use the code in action functions to change a independent
action's layout file.

public function indexAction()
{
    Zend_Layout::getMvcInstance()->setLayout('some_special_layout');

        ...
}
 
If you want to, or need a different layout file for one action vs another.
 
Also, you can also disable the layout if you don't want it at all:
Zend_Layout::getMvcInstance()->disableLayout();

________________________________

From: Jurian Sluiman [mailto:[hidden email]]
Sent: Tuesday, November 03, 2009 12:10 PM
To: [hidden email]
Subject: Re: [fw-general] Change the layout from module bootstrap


Place an init method inside the class like this or something:





>public function _initLayout ()
>{
> Zend_Layout::getMvcInstance()->setLayout($myLayoutName)
>}





Regards, Jurian
--
Jurian Sluiman
Soflomo.com





Op Tuesday 03 November 2009 17:25:17 schreef Simeon Goranov:

> Hello,
> I've got a module bootstrap file Admin_Bootstrap where the module is
> "admin". I'm wondering how to change the default layout, which in my
> case is layout.phtml only for the admin module. In my opinion I have to
> do it into the bootstrap file, because in this way the change will
> affect all of the module controllers. Do you have any idea how to solve
> the problem ?
>
> Regards,
> S.G.

Brenton Alker-3

Re: Change the layout from module bootstrap

Reply Threaded More More options
Print post
Permalink
In reply to this post by Simeon Goranov-3
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Simeon Goranov wrote:
> Hello,
> I've got a module bootstrap file Admin_Bootstrap where the module is
> "admin". I'm wondering how to change the default layout, which in my
> case is layout.phtml only for the admin module. In my opinion I have to
> do it into the bootstrap file, because in this way the change will
> affect all of the module controllers. Do you have any idea how to solve
> the problem ?

You can't do it in your bootstrap as has been suggested by others. The
bootstrap for *all* modules is run for *all* requests; even if the
default module is requested, the admin bootstrap is still run. At the
bootstrap stage routing has not yet been done, so the module being
requested has not yet been determined. So, if you change the layout in
any bootstrap it will be changed for every module.

The most common solution I've seen to this is to use a pre-dispatch
front controller plugins or action helper. Both of these will allow you
to check the current request after routing and take the necessary steps
to change the layout.

- --

Brenton Alker
PHP Developer - Brisbane, Australia

http://blog.tekerson.com/
http://twitter.com/tekerson

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkrww2MACgkQ7bkAtAithus/rACfekgNOHJGmuOtuSC1UjFwMYLY
GuQAniCsI0NM2PMTB7UbxPQtL8euVcIV
=f7m9
-----END PGP SIGNATURE-----
scss

Re: Change the layout from module bootstrap

Reply Threaded More More options
Print post
Permalink
Hello,
Here is a sample code from my projects. I tested and it works.
plugin file code:

        class My_Plugin_ChangeModuleLayout extends Zend_Controller_Plugin_Abstract
        {
            public function preDispatch(Zend_Controller_Request_Abstract $request)
            {
                $module      = $request->getModuleName();
                if ($module == 'admin') {
                    Zend_Layout::getMvcInstance()->setLayout('layout.admin');
                }
            }
        }

bootstrap code:
        protected function _initPlugins()
        {
                $this->bootstrap('frontController');
                $frontController = $this->getResource('frontController');
                $frontController->registerPlugin(new My_Plugin_ChangeModuleLayout());
        }

Here is the pastebin link of the same code:
http://pastebin.com/m62bd23b1

scs

On Wed, Nov 4, 2009 at 1:57 AM, Brenton Alker <[hidden email]> wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Simeon Goranov wrote:
>> Hello,
>> I've got a module bootstrap file Admin_Bootstrap where the module is
>> "admin". I'm wondering how to change the default layout, which in my
>> case is layout.phtml only for the admin module. In my opinion I have to
>> do it into the bootstrap file, because in this way the change will
>> affect all of the module controllers. Do you have any idea how to solve
>> the problem ?
>
> You can't do it in your bootstrap as has been suggested by others. The
> bootstrap for *all* modules is run for *all* requests; even if the
> default module is requested, the admin bootstrap is still run. At the
> bootstrap stage routing has not yet been done, so the module being
> requested has not yet been determined. So, if you change the layout in
> any bootstrap it will be changed for every module.
>
> The most common solution I've seen to this is to use a pre-dispatch
> front controller plugins or action helper. Both of these will allow you
> to check the current request after routing and take the necessary steps
> to change the layout.
>
> - --
>
> Brenton Alker
> PHP Developer - Brisbane, Australia
>
> http://blog.tekerson.com/
> http://twitter.com/tekerson
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.9 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
>
> iEYEARECAAYFAkrww2MACgkQ7bkAtAithus/rACfekgNOHJGmuOtuSC1UjFwMYLY
> GuQAniCsI0NM2PMTB7UbxPQtL8euVcIV
> =f7m9
> -----END PGP SIGNATURE-----
>
Simeon Goranov-3

Re: Change the layout from module bootstrap

Reply Threaded More More options
Print post
Permalink
In reply to this post by Brenton Alker-3
В 09:57 +1000 на 04.11.2009 (ср), Brenton Alker написа:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Simeon Goranov wrote:
> > Hello,
> > I've got a module bootstrap file Admin_Bootstrap where the module is
> > "admin". I'm wondering how to change the default layout, which in my
> > case is layout.phtml only for the admin module. In my opinion I have to
> > do it into the bootstrap file, because in this way the change will
> > affect all of the module controllers. Do you have any idea how to solve
> > the problem ?
>
> You can't do it in your bootstrap as has been suggested by others. The
> bootstrap for *all* modules is run for *all* requests; even if the
> default module is requested, the admin bootstrap is still run. At the
> bootstrap stage routing has not yet been done, so the module being
> requested has not yet been determined. So, if you change the layout in
> any bootstrap it will be changed for every module.
>
> The most common solution I've seen to this is to use a pre-dispatch
> front controller plugins or action helper. Both of these will allow you
> to check the current request after routing and take the necessary steps
> to change the layout.
>

Thanks for the code !
I'm wondering what to do if I've got code which is specific for one the
modules. I mean something like checking for authorization in admin
module, for example. What is the best practice to resolve this
situation ?
Using plugins, extend each of the controllers from admin with a class
like Admin_Bootstrap_Controller or there is another solutions ?

Regards,
S.G.


> - --
>
> Brenton Alker
> PHP Developer - Brisbane, Australia
>
> http://blog.tekerson.com/
> http://twitter.com/tekerson
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.9 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
>
> iEYEARECAAYFAkrww2MACgkQ7bkAtAithus/rACfekgNOHJGmuOtuSC1UjFwMYLY
> GuQAniCsI0NM2PMTB7UbxPQtL8euVcIV
> =f7m9
> -----END PGP SIGNATURE-----


weierophinney

Re: Change the layout from module bootstrap

Reply Threaded More More options
Print post
Permalink
-- Simeon Goranov <[hidden email]> wrote
(on Wednesday, 04 November 2009, 10:37 AM +0200):
> В 09:57 +1000 на 04.11.2009 (ср), Brenton Alker написа:
> > Simeon Goranov wrote:
> Thanks for the code !
> I'm wondering what to do if I've got code which is specific for one the
> modules. I mean something like checking for authorization in admin
> module, for example. What is the best practice to resolve this
> situation ?
> Using plugins, extend each of the controllers from admin with a class
> like Admin_Bootstrap_Controller or there is another solutions ?

I'd have the module's bootstrap register a plugin with appropriate hooks
(dispatchLoopStartup() and/or preDispatch()), and examine the request
object to determine if that module is requested.

--
Matthew Weier O'Phinney
Project Lead            | [hidden email]
Zend Framework          | http://framework.zend.com/