I can't figure out to generate Url from everywhere i want to, in zend 2
I get action and controller so i try this:
$this->url('myControllerName', array('action' => 'myActionName'));
But this return an object, i just want the full URL string of this route
Somebody can help me to find the proper way?
EDIT : according to Stoyan, maybe i made a mistake on my route. here is the part of my module.config
'router' => array (
'routes' => array (
'indexqvm' => array (
'type' => 'segment',
'options' => array (
'route' => '/Indexqvm[/:action][/:id_event]',
'constraints' => array (
'action' => '[a-zA-Z][a-zA-Z0-9_-]+',
'id_event' => '[0-9]+'
),
'defaults' => array (
'controller' => 'Qvm\Controller\Indexqvm',
'action' => 'index'
)
)
),
And my call :
echo $this->url('indexqvm', array('action' => 'list-index'));
the error :
Catchable fatal error: Object of class Zend\Mvc\Controller\Plugin\Url could not be converted to string
Use the echo before calling $this->url(...) (see bellow) and this will display the whole URL.
<?php echo $this->url('route-name', $urlParams, $urlOptions); ?>
Note that the first paramter of url() is the name of the route as specified in your [module]/config/module.config.php file.
See this for more information about ZF2's URL view helper.
EDIT in response to the question edit:
The above section is related to using the URL view helper.
If you want a URL in the controller then you need the URL controller plugin.
<?php $url = $this->url()->fromRoute('route-name', $params, $options); ?>
This is the reference to the ZF2 manual for this controller plugin.
Hope this helps :)
Stoyan
You can use this in the .phtml file
echo $this->url('HelloWorld/default', array('controller'=>'Index', 'action'=>'registration'));
Where HelloWorld/default is the routing and the remaining is the controller and its action and also you can send the others parameter adding just in array as key and value pair.
Related
I'm having trouble finding how to get the Urls from named routes inside a Phalcon PhP controller. This is my route:
$router->add(
'/admin/application/{formUrl:[A-Za-z0-9\-]+}/{id:[A-Za-z0-9\-]+}/detail',
[
'controller' => 'AdminApplication',
'action' => 'detail'
]
)->setName("application-details");
I want to get just the Url, example: domain.com/admin/application/form-test/10/detail . With the code below I can get the html to create a link, the same result of the link_to.
$url = $this->tag->linkTo(
array(
array(
'for' => 'application-details',
'formUrl' => $form->url,
'id' => $id
),
'Show'
)
);
The result I want is just the Url. I'm inside a controller action. I know it must be really simple, I just can't find an example. Can you help me?
Thanks for any help!
You should use the URL helper. Example:
$url = $this->url->get(
[
'for' => 'application-details',
'formUrl' => $form->url,
'id' => $id,
],
[
'q' => 'test1',
'qq' => 'test2',
]
);
You can pass second array for query string params if needed.
According to your route definition, the above should output something like:
/admin/application/form-url/25/detail?q=test1&qq=test2
More info of Generating URIs in the docs.
I'm struggling on this for 2 hours now. I've managed to create a route with parameter (named url) like /login/url:
'login' => array(
'type' => 'segment',
'options' => array(
'route' => '/login[/:url]',
'defaults' => array(
'controller' => 'my_controller',
'action' => 'login',
),
),
),
However I'd like to have an URL which looks like /login?url=foo. I've tried something like:
'route' => '/login[?url=:url]',
But it does not work. Any idea how to achieve this on Zend Framework 2 ?
Thanks a lot!
EDIT:
Trying something else like:
// onBootstrap method --> redirect to login page with request url as param
$url = $router->assemble(
array('url', $e->getRequest()->getRequestUri()),
array('name' => 'login')
);
In controller (login action):
$request = $this->getRequest();
var_dump($request); exit;
I don't see the requested URL anywhere... any suggestion?
I don't think you should put your query string segment in your route. It would seem reasonable to have your route to be just /login and then manage your query string parameter in the controller.
Otherwise, but I don't recommend it since it is deprecated, you could try to use the Query router.
I have some simple pagination set up in my controller like so:
public function index() {
$this->set('items', $this->paginate());
}
In my view I'm using the Paginator helper to output numbered links:
echo $this->Paginator->numbers(array(
'separator' => '',
'tag' => 'li',
'currentTag' => 'a',
'currentClass' => 'active'
));
This all works fine, however I want to use a custom URL for the paginated links. I added this to my routes.php file:
Router::connect('/things/:page', array('controller' => 'things', 'action' => 'index'), array('page' => '[0-9]+'));
Now the links outputted by the Paginator helper are the way I want. They look like http://mysite.com/things/2, http://mysite.com/things/3 etc.
But when I click the links the Paginator in my controller doesn't seem to recognize it's on a certain page, as I just get the first page's results shown. I'm guessing I need to somehow pass the page number to $this->paginate(), but I don't know what the best method is. Is there a way for Cake to get the page number automatically if I modify my route?
Thanks!
Since the Pagination component by default expect named parameter 'page:1' you need somehow to pass same variable in index.
if you make print of $this->request->params in your controller, you will see that it's missing.
See that example how you can pass named parameters in the Router:
Router::connect(
'/:controller/:action/*',
array(),
array(
'named' => array(
'wibble',
'fish' => array('action' => 'index'),
'fizz' => array('controller' => array('comments', 'other')),
'buzz' => 'val-[\d]+'
)
)
);
For more info see this section in Cakephp book
I'm using Zend Framework 1.12 and have this route:
$router->addRoute('item_start',
new Zend_Controller_Router_Route_Regex(
'(foo|bar|baz)',
array(
'module' => 'default',
'controller' => 'item',
'action' => 'start'
),
array(
1 => 'area'
),
'%s'
)
);
Problem is, when I call '/foo' and use the Url Helper in the View, it doesn't give me any parameters:
$this->url(array("page"=>1));
// returns '/foo' (expected '/foo/page/1')
$this->url(array("page"=>1), "item_start", true);
// also returns '/foo'
Any idea how to get the page-parameter into the URL? I can't use the wildcard like in the standard route, can't I?
In addition to David's suggestions, you could change this route to use the standard route class, and then keep the wildcard option:
$router->addRoute('item_start',
new Zend_Controller_Router_Route(
':area/*',
array(
'module' => 'default',
'controller' => 'item',
'action' => 'start'
),
array(
'area' => '(foo|bar|baz)'
)
)
);
// in your view:
echo $this->url(array('area' => 'foo', 'page' => 1), 'item_start');
Your Regex route doesn't have a page parameter, so when the url view-helper ends up calling Route::assemble() with the parameters you feed it, it ignores your page value.
The two choices that come to mind are:
Modify your regex to include a (probably optional with default value) page parameter
Manage the page parameter outside of your route in the query string.
I have one route that looks like this:
Router::connect('/Album/:slug/:id',array('controller' => 'albums', 'action' => 'photo'),array('pass' => array('slug','id'),'id' => '[0-9]+'));
and another like this:
Router::connect('/Album/:slug/*',array('controller' => 'albums','action' => 'contents'),array('pass' => array('slug')));
for what doesn't match the first. In the 'contents' action of the 'albums' controller, I take care of pagination myself - meaning I retrieve the named parameter 'page'.
A URL for the second route would look like this:
http://somesite.com/Album/foo-bar/page:2
The Above URL indeed works, but when I try to use the HTML Helper (url,link) to output a url like this, it appends the controller and action to the beginning, like this:
http://somesite.com/albums/contents/Album/foo-bar/page:2
Which i don't like.
The code that uses the HtmlHelper is as such:
$html->url(array('/Album/' . $album['Album']['slug'] . '/page:' . $next))
See below url it is very help full to you
http://book.cakephp.org/2.0/en/development/routing.html
Or read it
Passing parameters to action
When connecting routes using Route elements you may want to have routed elements be passed arguments instead. By using the 3rd argument of Router::connect() you can define which route elements should also be made available as passed arguments:
<?php
// SomeController.php
public function view($articleId = null, $slug = null) {
// some code here...
}
// routes.php
Router::connect(
'/blog/:id-:slug', // E.g. /blog/3-CakePHP_Rocks
array('controller' => 'blog', 'action' => 'view'),
array(
// order matters since this will simply map ":id" to $articleId in your action
'pass' => array('id', 'slug'),
'id' => '[0-9]+'
)
);
And now, thanks to the reverse routing capabilities, you can pass in the url array like below and Cake will know how to form the URL as defined in the routes:
// view.ctp
// this will return a link to /blog/3-CakePHP_Rocks
<?php
echo $this->Html->link('CakePHP Rocks', array(
'controller' => 'blog',
'action' => 'view',
'id' => 3,
'slug' => 'CakePHP_Rocks'
));