I am converting a ZF2 "filename.phtml" view to a "filename.twig" view. I am needing help knowing how to pass a variable to a partial. The nature of the partial provides a variety of "social network" share type buttons. What I am giving the partial is the URL of the web page being accessed to "share".
The code I am trying to convert:
echo $this->partial('partial/socialnetworkshare', array('web_page' => $this->url('main-prayersfortoday', array('prayer_reference' => $this->prayer_reference ))));
I haven't correctly figured out how to pass the value of "$this->prayer_reference". This is what I have tried:
{{ partial('partial/socialnetworkshare', {'web_page' : url('main-prayersfortoday') , 'prayer_reference' : prayer_reference }) }}
prayer_ministry is to match this route:
'main-prayersfortoday' => array(
'type' => 'segment',
'options' => array(
'route' => '/prayers-for-today[/:prayer_reference]',
'constraints' => array(
'prayer_reference' => '[0-9]*'
),
'defaults' => array(
'controller' => 'Main\Controller\Main',
'action' => 'prayersfortoday'
)
)
),
The value of "prayer_ministry" is passing through this controller:
$viewModel
->setTemplate('main/main/prayersfortodaydocument')
->setVariable('prayer_reference', $this->params()->fromRoute('prayer_reference'))
->setVariable('document', $document);
I very much appreciate your help. Thank you.
I think what you're after is something like this, using the include tag:
{% include 'partial/socialnetworkshare' with {
'web_page': path('main-prayersfortoday'),
'prayer_reference': prayer_reference
} %}
In Symfony, we have Twig extensions which provide the Twig path() function similar to $this->url() in Zend. You may need to register you own Twig extension to provide this in Zend.
Related
I have some component I want to make it separate currently my components looks like
-protected/components
-GeneralFunction.php
-CustomFunction.php
and my config I have called:
'components' => array(
'general' => array('class' => 'GeneralFunction'),
'custom' => array('class' => 'CustomFunction'),
),
Above code is working fine But I want to separate frontend and backend of my components like:
-protected/components
-frontend
-GeneralFunction.php
-CustomFunction.php
-backend
-GeneralFunction.php
-CustomFunction.php
and my config I am calling:
'components' => array(
'general2' => array('class' => 'frontend.GeneralFunction'),
),
TestController.php
function actionTestComponent(){
echo Yii::app()->general2->test(); exit;
}
I am getting this error message:
2017/12/19 11:17:54 [error] [exception.CException] CException: Alias "frontend.GeneralFunction" is invalid. Make sure it points to an existing directory or file. in C:\xampp\htdocs\yii\framework\YiiBase.php:348
Please help me..
After lots of research I found this:
'general2' => array('class' => 'application.components.frontend.GeneralFunction'),
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 obviously have a fundamental misunderstanding of how pagination works in CakePHP.
I have the following route set up which shows all posts in a category:
Router::connect('/:parent/:category',
array('controller' => 'posts', 'action' => 'viewCategory'),
array('parent' => '[a-z0-9-]+', 'category' => '[a-z0-9-]+'));
The pages work fine, however the pagination helper is outputting the wrong links for pagination.
I'm using $this->Paginator->numbers().
It's outputting links in this format: mysite.com/posts/viewCategory?page=2
rather than like this: mysite.com/parent-category/sub-category?page=2.
I've tried adding the following route after the first one and it still doesn't work:
Router::connect('/:parent/:category/:page',
array('controller' => 'posts', 'action' => 'viewCategory'),
array('parent' => '[a-z0-9-]+',
'category' => '[a-z0-9-]+',
'page' => '[0-9]+'));
For reference, my pagination options set in my view are as so:
<?php $this->Paginator->options(
array('url' =>
array('controller' => 'posts', 'action' => 'viewCategory')
)); ?>
What am I doing wrong here?
You are setting the url yourself
This is your paginator options call:
<?php
$this->Paginator->options(array(
'url' => array(
'controller' => 'posts',
'action' => 'viewCategory'
)
));
?>
Where you are overriding the current url - and explicitly requesting that the paginator uses the the '/posts/viewCategory' url (with no arguments) as it's base url.
Just don't define the url
Simply don't call options and the helper will use the current url - that should mean that if the current url is:
/parent-category/sub-category
Then page 2 will be (assuming you are using the paramType option to use GET arguments rather than named parameters):
/parent-category/sub-category?page=2
If that's not the case there's information missing from the question; it's important to distinguish between "vanity routes not being used" and "the url is not equivalent (the current situation).
Just had a battle fixing something similar and came across this post. Though old, but I think my answer might save someone the time I had to spend fixing it.
Basically, what you need to do is call the Paginator->options() before Paginator->numbers(), thus:
$this->Paginator->options(
array(
'controller' => 'parent-category',
'action' => 'sub-category'
)
);
Though the controller and action do not exist, it just tricks CakePHP to use them "AS IS", since the reverse routing isn't working!
And for those (like me), who want have set up a route similar to
Router::connect(
'/go/page:id',
array(
'controller' => 'blog',
'action' => 'paginated'
)
);
There might be difficulty setting up the Paginator options. This, however, worked for me:
$this->Paginator->options(
array(
'controller' => 'go',
'action' => '/'
)
);
I guess you know why it worked ;)
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.
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.