Quantcast

Setting the base url in ZF2-MVC

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Setting the base url in ZF2-MVC

Mon Zafra
Hello,
I'm trying to learn ZF2 MVC at the moment. At first I had trouble
getting the index page to show up until I tried changing the routes to
include the base uri. How do I change it at the request level? I did a
bit of digging and saw nothing on `HttpRequest`. I did find
`Zend\Uri\Http::makeRelative()`, but how do I call this method on the
`HttpRequest`'s uri object during bootstrap or configuration? Any
guidance would be greatly appreciated.

Thanks,
    -- Mon

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Setting the base url in ZF2-MVC

EmanueleDeserti
I'm looking for the same things but I can't find a place where to put a "setBaseUrl()" call...

Or maybe a configuration directive in some of "config" or "autoload" files...

Il giorno 28/ott/2011, alle ore 00:42, Mon Zafra ha scritto:

> Hello,
> I'm trying to learn ZF2 MVC at the moment. At first I had trouble
> getting the index page to show up until I tried changing the routes to
> include the base uri. How do I change it at the request level? I did a
> bit of digging and saw nothing on `HttpRequest`. I did find
> `Zend\Uri\Http::makeRelative()`, but how do I call this method on the
> `HttpRequest`'s uri object during bootstrap or configuration? Any
> guidance would be greatly appreciated.
>
> Thanks,
>     -- Mon
>
> --
> List: [hidden email]
> Info: http://framework.zend.com/archives
> Unsubscribe: [hidden email]
>
>

-----------------------------------------------------------------------------------------------------------------------
Emanuele Deserti
Responsabile Area Sviluppo
Netwing Srl
Via Zucchini 79, 44122 - Ferrara - Italy
P.Iva: 01744830389

Tel: +39.0532.1912002
Fax: +39.0532.54481
Email: [hidden email]
Skype: emanueledeserti



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Setting the base url in ZF2-MVC

weierophinney
Administrator
In reply to this post by Mon Zafra
-- Mon Zafra <[hidden email]> wrote
(on Friday, 28 October 2011, 06:42 AM +0800):
> I'm trying to learn ZF2 MVC at the moment. At first I had trouble
> getting the index page to show up until I tried changing the routes to
> include the base uri. How do I change it at the request level? I did a
> bit of digging and saw nothing on `HttpRequest`. I did find
> `Zend\Uri\Http::makeRelative()`, but how do I call this method on the
> `HttpRequest`'s uri object during bootstrap or configuration? Any
> guidance would be greatly appreciated.

This is actually the job of the Router. If you use
Zend\Mvc\Router\TreeStackRouter (which is what the
ZendSkeletonApplication uses), you can set it on the router itself:

    $router->setBaseUrl($baseUrl);

The bigger question is where to do this. My suggestion is to do the
following in your modules/Application/Module.php:

    use Zend\EventManager\StaticEventManager;

    class Module
    {
        public function init()
        {
            // keep what was there previously...
           
            $events = StaticEventManager::getInstance();
            $events->attach('bootstrap', 'bootstrap', array($this, 'registerBaseUrl'));
        }

        public function registerBaseUrl($e)
        {
            $modules = $e->getParam('modules');
            $config  = $modules->getMergedConfig();
            $app     = $e->getParam('application');
            $router  = $app->getRouter();
            $router->setBaseUrl($config->base_url);
        }
    }

Then, in your modules/Application/configs/module.config.php, ensure you
have a "base_url" key set:

    'base_url' => '/base_dir/'

Once this is in place, you should be all set.

--
Matthew Weier O'Phinney
Project Lead            | [hidden email]
Zend Framework          | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Setting the base url in ZF2-MVC

Mon Zafra
Thanks Matthew, it's perfect.

    -- Mon



On Fri, Oct 28, 2011 at 11:26 PM, Matthew Weier O'Phinney
<[hidden email]> wrote:

> -- Mon Zafra <[hidden email]> wrote
> (on Friday, 28 October 2011, 06:42 AM +0800):
>> I'm trying to learn ZF2 MVC at the moment. At first I had trouble
>> getting the index page to show up until I tried changing the routes to
>> include the base uri. How do I change it at the request level? I did a
>> bit of digging and saw nothing on `HttpRequest`. I did find
>> `Zend\Uri\Http::makeRelative()`, but how do I call this method on the
>> `HttpRequest`'s uri object during bootstrap or configuration? Any
>> guidance would be greatly appreciated.
>
> This is actually the job of the Router. If you use
> Zend\Mvc\Router\TreeStackRouter (which is what the
> ZendSkeletonApplication uses), you can set it on the router itself:
>
>    $router->setBaseUrl($baseUrl);
>
> The bigger question is where to do this. My suggestion is to do the
> following in your modules/Application/Module.php:
>
>    use Zend\EventManager\StaticEventManager;
>
>    class Module
>    {
>        public function init()
>        {
>            // keep what was there previously...
>
>            $events = StaticEventManager::getInstance();
>            $events->attach('bootstrap', 'bootstrap', array($this, 'registerBaseUrl'));
>        }
>
>        public function registerBaseUrl($e)
>        {
>            $modules = $e->getParam('modules');
>            $config  = $modules->getMergedConfig();
>            $app     = $e->getParam('application');
>            $router  = $app->getRouter();
>            $router->setBaseUrl($config->base_url);
>        }
>    }
>
> Then, in your modules/Application/configs/module.config.php, ensure you
> have a "base_url" key set:
>
>    'base_url' => '/base_dir/'
>
> Once this is in place, you should be all set.
>
> --
> Matthew Weier O'Phinney
> Project Lead            | [hidden email]
> Zend Framework          | http://framework.zend.com/
> PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc
>
> --
> List: [hidden email]
> Info: http://framework.zend.com/archives
> Unsubscribe: [hidden email]
>
>
>

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Setting the base url in ZF2-MVC

HHGAG
Could it be, that BaseUrl View Helper is out of sync?

Method getBaseUrl depands on \Zend\Controller\Front which isn't available in Beta package, but that would return null at the current state
Hasan H. Gürsoy (HHGAG)
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Setting the base url in ZF2-MVC

weierophinney
Administrator
-- HHGAG <[hidden email]> wrote
(on Monday, 07 November 2011, 12:22 AM -0800):
> Could it be, that BaseUrl View Helper is out of sync?

Yes, it could, and is the case.

If you use the TreeRouteStack, you can set the base URL on the router
itself, and then the url() view helper (in the ZendSkeletonApplication
or in current master)  will automatically use it.

> Method getBaseUrl depands on \Zend\Controller\Front which isn't
> available in Beta package, but that would return null at the current
> state

Right, and I'm actually in the process of refactoring all components
that depend on the front controller; I hope to have something to
review/push by no later than tomorrow.

--
Matthew Weier O'Phinney
Project Lead            | [hidden email]
Zend Framework          | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Setting the base url in ZF2-MVC

Rafael Hengles-2
In reply to this post by weierophinney
Hello all,

Is this still the current method for Beta 5? Sorry, I'm a total
beginner in ZF2 and I'm trying to start a new project with the
skeleton Application and Module. I tried the method below but it
didn't seem to work.

Thanks! :)



On Fri, Oct 28, 2011 at 1:26 PM, Matthew Weier O'Phinney
<[hidden email]> wrote:

>
> This is actually the job of the Router. If you use
> Zend\Mvc\Router\TreeStackRouter (which is what the
> ZendSkeletonApplication uses), you can set it on the router itself:
>
>     $router->setBaseUrl($baseUrl);
>
> The bigger question is where to do this. My suggestion is to do the
> following in your modules/Application/Module.php:
>
>     use Zend\EventManager\StaticEventManager;
>
>     class Module
>     {
>         public function init()
>         {
>             // keep what was there previously...
>
>             $events = StaticEventManager::getInstance();
>             $events->attach('bootstrap', 'bootstrap', array($this, 'registerBaseUrl'));
>         }
>
>         public function registerBaseUrl($e)
>         {
>             $modules = $e->getParam('modules');
>             $config  = $modules->getMergedConfig();
>             $app     = $e->getParam('application');
>             $router  = $app->getRouter();
>             $router->setBaseUrl($config->base_url);
>         }
>     }
>
> Then, in your modules/Application/configs/module.config.php, ensure you
> have a "base_url" key set:
>
>     'base_url' => '/base_dir/'
>
> Once this is in place, you should be all set.
>
> --
> Matthew Weier O'Phinney
> Project Lead            | [hidden email]
> Zend Framework          | http://framework.zend.com/
> PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc
>
> --
> List: [hidden email]
> Info: http://framework.zend.com/archives
> Unsubscribe: [hidden email]
>
>

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Loading...