We're using Zend Framework 2 and use toRoute within our controllers to redirect to various locations, for example $this->redirect()->toRoute('home');.
Is there anyway to have this redirect to https instead of http using this method or an alternative method?
Thank you!
In order to use https in your route you need to use the Zend\Mvc\Router\Http\Scheme router. Specifying the configuration for such route is not very different from the other routes. You need to specify the route type as Scheme and add an option 'scheme' => 'https' in your router configuration in module.config.php.
Here is an example:
return array(
'router' => array(
'routes' => array(
'routename' => array(
'type' => 'Scheme', // <- This is important
'options' => array(
'route' => '/url',
'scheme' => 'https', // <- and this.
'defaults' => array(
'__NAMESPACE__' => 'MdlNamespace\Controller',
'controller' => 'Index',
'action' => 'someAction',
),
),
),
// the rest of the routes
),
),
// the rest of the module config
);
If you have the route routename configured like above, this: $this->redirect()->toRoute('routename'); will work.
See this for reference to the ZF2's manual.
Hope this helps :)
Stoyan
Related
I'm trying to create a dynamic route in a ZF2 project. It will be something like "domain.com/companyurl/products". The company url is dynamic. I did it:
'company' => array(
'type' => 'Segment',
'options' => array(
'route' => '[/:company]',
'defaults' => array(
'controller' => 'IndexController',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
...
),
),
But I always have to pass the company parameter in a route.
$this->url('company/products', array('company' => 'companyurl'));
Is there some way to specify a base route at the runtime, like a base url, then all route will follow it? Something like this:
$this->url('products');
or
$this->url('company/products');
In the both cases I already specified the base route value.
I hope you understand what I mean. Thanks.
There is a $reuseRouteParams option that you can use in the URL helper:
$this->url($name, $params, $options,$reuseMatchedParameters);
If you set this to true it will reuse the previous route match value of companyUrl.
You can read more on this in the docs here.
I'm just starting off with Zend Framework, and I'm not quite sure what I'm doing wrong with the URI routing.
I'm starting with an initial Zend Framework project in Zend Studio based in my htdocs folder (I'm using Zend Server as well on Windows 7). Everything up to there seems to be working fine getting the index page up (it's running out of the /public/ subdirectory).
But when I try to add a module though, in this case called Users with a controller called Index, and following the instructions in getting that configured, I'm not sure what I should be putting in the URI to get it to route to it's view. I've tried just about every configuration of URI combinations that I can think of (localhost:80/public/users, localhost:80/public/users/index, localhost:80/users, etc)
I'm not getting a routing error, but just a plain 404 page.
Do I need to set the public folder as the root? Or is there something else I need to do to get the routing to work?
~edit in response to bitWorking
It looks like it does automatically add it to the application.config.php. But here is the module.config.php of the Users module
'router' => array(
'routes' => array(
'users' => array(
'type' => 'Literal',
'options' => array(
// Change this to something specific to your module
'route' => '/index',
'defaults' => array(
// Change this value to reflect the namespace in which
// the controllers for your module are found
'__NAMESPACE__' => 'Users\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
// This route is a sane default when developing a module;
// as you solidify the routes for your module, however,
// you may want to remove it and replace it with more
// specific routes.
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
),
),
),
Now I do see where it's guiding you to customize the routes. I've experimented with this as well, but still am not sure what I should set them to. Much closer though.
If you want to call the Index controller in your Users module with /users you have to name the route accordingly:
...
'users' => array(
'type' => 'Literal',
'options' => array(
// Change this to something specific to your module
'route' => '/users',
---------
...
Else please control the application.config.php. It should look like:
return array(
'modules' => array(
'Application',
'Users',
),
...
So the Url's should look like:
localhost/public/users -> Users/Controller/IndexController/indexAction
localhost/public/users/foo -> Users/Controller/FooController/indexAction
localhost/public/users/foo/bar -> Users/Controller/FooController/barAction
I implemented a form for placing new orders in Zend Framework 2 and after submitting the form I should redirect to another route and take the orders.id variable in another controller.
I tried using $this->redirect()->toRoute('confirm', array('param'=>$orderId)); but it is not working at all.
Maybe I do not know how to get that parameter in another confirmAction controller.
Please give me some examples. Thank you very much.
1) Since this is a routing issue, show what you have for the route in the module.config.php file. You might not have the "param" constraint configured properly in your config if I had to guess.
It should look something like this:
'confirm' => array(
'type' => 'segment',
'options' => array(
'route' => '/controller_name/confirm[/][:param][/]',
'constraints' => array(
'param' => '[0-9]*'
),
'defaults' => array(
'__NAMESPACE__' => 'your_namespace', // ex. Application\Controller
'action' => 'confirm', // or whatever action you're calling
'controller' => 'controller_name' // ex.
),
),
),
Is there a way to pass parameters with redirect in ZF2 like
return $this->redirect()->toRoute('account', array('test' => 'ok'));
and if possible, how can I get the test parameter?
PS. test is not a parameter mapped to the route. account is my controller and it has a default action, so I don't need to specify the action in redirect.
I think you can do the following:
Say your module is Myaccount and your controller is AccountController and your action is index (your default action I presume). So in your module.config.php you'd have something like this:
return array(
'controllers' => array(
'invokables' => array(
'Account' => 'Myaccount\Controller\AccountController',
Then in some action you can pass parameters with redirect in ZF2 style like this:
return $this->redirect()->toRoute('account', array(
'controller' => 'account',
'action' => 'index',
'test' => $testVal // in this case "okay"
));
Oh yes, your route in your module.config.php is also important:
'router' => array(
'routes' => array(
'account' => array(
'type' => 'segment',
'options' => array(
'route' => '/account[/:action][/:test]',
Because that's how ZF2 knows where to go and what to append to it, in our case it would lead to "../account/index/test/okay" as okay was our value for "test".
Hope it helps :)
I'm trying to define some console routes for my ZF2 application as described here http://packages.zendframework.com/docs/latest/manual/en/modules/zend.console.routes.html
in the module config I have:
'console' => array(
'router' => array(
'routes' => array(
'user-set-password' => array(
'options' => array(
'route' => 'user password <username> <password>',
'defaults' => array(
'controller' => 'User\Profile',
'action' => 'setpassword'
),
),
),
),
),
),
but it seems to never match the route as it always prints the usage information. also simple routes like just 'test' won't be matched.
(when I write some crap into the route parameter, the execution fails with an Zend\Mvc\Router\Exception\InvalidArgumentException so it recognizes the console route when loading the module)
is it my fault or maybe a bug in the latest zf2 version?
I just found the solution in an inconsistent interface for the route definitions:
it works if you provide the following schema for the controller:
'controller' => 'User\Controller\Profile'
would be better to be able to define it in the same way as http routes:
'defaults' => array(
'__NAMESPACE__' => 'User\Controller',
'controller' => 'Profile',
'action' => 'setpassword',
),
just opened an issue for that: http://framework.zend.com/issues/browse/ZF2-515