hi i am new in zend framework 2.2.0. i want to create the a link that go to delete page right now i am passing only id in the url so i want to pass another id in it.
Add to Trash
right now in this link message id is passing i also want to pass one more id named "did" in this link
Add to Trash
how can i get this ?
thanks in advance
You should use url view helper's third argument ($options) to pass your variables in the query string. Example:
$route = 'message';
$param = array('action' => 'delete');
$opts = array(
'query' => array(
'id' => $message->message_id,
'did' => $message->deliver_id
)
);
$this->url($route, $params, $opts);
You have to add "did" to your message route like this:
'router' => array(
'routes' => array(
'message' => array(
'type' => 'segment',
'options' => array(
'route' => '/:action/:id[/:did]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]+',
'id' => '[0-9]+',
'did' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Application\Controller\Index',
),
),
),
),
),
echo $this->url('message',array('action'=>'delete', 'id' => $message->message_id,'did'=>$message->deliver_id);
// output: /delete/1/2
Related
I am using ZendFramework 2.x and trying to add a route to some existing ones. Moreover I want to put some parameters in die URL as well. If I use the segment-type for my new route ('showroom') I am able to call my new URL and will get forwarded to the corresponding view. Unfortunately I am not able to set some parameters in the URL. The other option is to use segment type in my module.config.php-file, but I will get some ZF-2-Exception, that my route is not configured correctly, this happens even before rendering my intro-view. Thanks in advance for showing me, how to combine segment and literal-type usage in route-child-route-combination or for telling me how to add parameters to literal type URLs.
Route-configuration in module.config.php of Module:
'router' => array(
'routes' => array(
'zfcuser' => array(
'type' => 'Literal',
'priority' => 1000,
'options' => array(
'route' => '/user',
'defaults' => array(
'controller' => 'zfcuser',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'showroom' => array(
'type' => 'Segment',
'options' => array(
'route' => '/:id',
'defaults' => array(
'controller' => 'zfcuser',
'action' => 'showRoom',
'id' => '1',
),
),
),
'login' => array(
'type' => 'Literal',
UPDATE: snippet which triggers the error (see comments)
<?php
$roomIndex = 1;
foreach($roomsPaginator->getCurrentItems() as $room){
$roomURL = 'zfcuser/showroom' . '/' . $roomIndex;
echo "<p>Name: " . $room['name'] . "; Luftfeuchtigkeit: " . $room['humidity'] . "; <a class='btn' href='" . $this->url($roomURL) . "'>Betreten »</a></p>";
$roomIndex++;
}
?>
I'm not sure if this is the cause of your error, but I don't think you're using the URL helper correctly. You don't build a URL and pass it to the helper, the idea is the helper builds the URL for you using the route name and params. You probably want something like this:
$roomIndex = 1;
foreach($roomsPaginator->getCurrentItems() as $room){
echo "<p>Name: " . $room['name'] . "; Luftfeuchtigkeit: " . $room['humidity'] . "; <a class='btn' href='" . $this->url('showroom', array('id' => $roomIndex)) . "'>Betreten »</a></p>";
$roomIndex++;
}
I'm trying to understand all the configuration necessary to get my routing working in Zend Framework 2, and I can't help but wonder if I am making this more complicated than necessary.
I am working on a simple app that will follow a very simple convention:
/:module/:controller/:action
I've already created and wired up my module, "svc" (short for "service)". I then created a second controller, the "ClientsController", and I can't get the routing to pass through my requests to, e.g., /svc/clients/list to ClientsController::listAction().
As I'm wading through hundreds of lines of configuration, in deeply nested arrays, I'm thinking--isn't there some way to just have a default mapping of my URLs to /:module/:controller/:action ?
Thanks for any assistance. I'm going off of the Zend Framework 2 Quick Start, which walked me through creating a new module and then adding a controller to that module. But when I tried to add second controller to that module, I am tripping over the routing.
Update: I didn't catch this the first time through, but apparently this is supposed to be a feature of the Zend Framework Skeleton app. From the quick start guide:
ZendSkeletonApplication ships with a “default route” that will likely
get you to this action. That route basically expects
“/{module}/{controller}/{action}”, which allows you to specify this:
“/zend-user/hello/world”
That's exactly what I want! But I can't get it to work.
It lists an incomplete module.config.php, with a comment at the bottom about putting "other configuration" here. I tried to figure out what that "other configuration" is, and wound up with this:
return array(
'svc' => array(
'type' => 'Literal',
'options' => array(
'route' => '/svc',
'defaults' => array(
'controller' => 'svc\Controller\Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'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(
),
),
),
),
),
'controllers' => array(
'invokables' => array(
'svc\Controller\Clients' => 'svc\Controller\ClientsController',
),
),
'view_manager' => array(
'template_path_stack' => array(
'album' => __DIR__ . '/../view',
),
),
);
JFYI, here is what my controller looks like.
namespace svc\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class ClientsController extends AbstractActionController {
public function indexAction() {
return new ViewModel();
}
public function anotherAction(){
return new ViewModel();
}
}
My routes are not working. I get "route not found" when I try to pull up any of my routes.
It lists an incomplete module.config.php, with a comment at the bottom about putting "other configuration" here. I tried to figure out what that "other configuration" is, and wound up with this:
If your module.config.php really looks like that then it won't work, routes is an array of routes defined in the router key, your config contains no such spec, try replacing it with this
return array(
// routes
'router' => array(
'routes' => array(
'svc' => array(
'type' => 'Literal',
'options' => array(
'route' => '/svc',
'defaults' => array(
'controller' => 'svc\Controller\Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'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(
// add the default namespace for :controllers in this route
'__NAMESPACE__' => 'svc\Controller',
),
),
),
),
),
),
),
'controllers' => array(
'invokables' => array(
'svc\Controller\Clients' => 'svc\Controller\ClientsController',
),
),
'view_manager' => array(
'template_path_stack' => array(
'album' => __DIR__ . '/../view',
),
),
);
I can redirect to matchable routes like $this->redirect()->toRoute('controller', array('action' => 'something', 'foo' => 'bar')), but how can I append a get query? I'm using it for a filter for a table. The filter can either be submitted on the page (form get method), or linked from another page (need to add get query).
try this :
'course' => array(
'type' => 'Segment',
'options' => array(
'route' => '/course[/:action[/:id]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '\d+'
),
'defaults' => array(
'__NAMESPACE__' => 'Home\Controller',
'controller' => 'course',
'action' => 'index',
'id' => 0
),
),
'may_terminate' => true,
'child_routes' => array(
'query' => array(
'type' => 'Query',
'options' => array(
'route' => '?[:filterByDepartment]'
),
),
),
),
make sure the route is met your need in the view :
echo $this->url('course/query', array('controller'=>'course', 'action'=>'index', 'id'=>0, 'filterByDepartment'=>'depA'));
if this is right, let's try the following in the controller action :
$this->redirect()->toRoute('course/query', array('controller'=>'course', 'action'=>'index', 'id'=>0, 'filterByDepartment'=>'depA'));
I just disvovered today that zf2 doesn't realy like when 2 controllers have the same name even if they are not in the same module.
However, I need to be able to call
localhost/users/types
and
localhost/messages/types
For the moment, my two controllers have the same names.
I also discovered that whatever the name of the module is, I always get the result of messages/types, even with localhost/nonexistingmodule/types oO
Here is what my module.config.php looks like :
return array(
'controllers' => array(
'invokales' => array(
'messages' => 'Messages\Controller\MessagesController,
'messages' => 'Messages\Controller\TypesController,
),
),
'di' => array(
'instance' => array(
'alias' = array(),
),
),
'router' => array(
'routes => array(
'restful' => array(
'type' => 'Zend\Mvc\Router\Http\Segment'
'options' => array(
'route' => '/Messages/:controller[.:formatter][/:id],
'constraints' => array(
'module' => '[a-zA-Z][a-zA-Z0-9_-]*',
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'formater' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[a-zA-Z0-9_-]*',
),
'defaults' => array(
'module' => 'Messages',
),
),
),
),
),
'view_manager' => array( ... ),
);
I have tried to set 'module' => 'Messages' in constraints (we never know :p) but I had a 404 error.
The module.config.php of the Tasks module is the same but for tasks.
I originaly had aliases but I removed them after I read somewhere this was not very recomanded.
One more thing, this is a REST API, all my controllers extends AbstractRestfulController (in case this is important)
Anyone has an idea of how to make my 2 url to work?
Thanks :)
Aliases for invokables or services and anything else should be unique. If they are not unique they may be overwritten by another module in the order the modules themselfes are loaded. That means: when setting up invokables or any sorts of aliases, make sure that the aliases are unique and meaningful. Personally i name my Controllers like this:
'invokables' => array(
'mymodule-controller-controllername' => 'Mymodule\Controller\ControllernameController'
)
Same with Services or any other sorts of aliases
'services' => array(
'mymodule-service-servicename' => 'Mymodule\Service\Classname'
)
The Documentation follows a Namespace-styled syntax like...
'invokables' => array(
'Mymodule\Controller\Controllername' => 'Mymodule\Controller\ControllernameController'
)
...which personally i find totally confusing, as it resembles a Namespace way too much and doesn't really auto-imply that it is just an alias / key
Now your comments-question i do not understand. You want to match one route to two different controllers? That would be impossible and senseless.
Answer Update with routing config
As for routing configuration you have several possible ways. Personally i put lots of effort into building up literal routes, since they are the fastest, but also require a lot of manual attention. Alternatively there is segment routes which inhibit a little more magic to them. I will cover the literal approach for you:
Module Messages
'controllers' => array(
'invokables' => array(
'messages-controller-index' => 'Messages\Controller\IndexController',
)
),
'router' => array(
'routes' => array(
'messages' => array(
'type' => 'literal',
'options' => array(
'route' => '/messages',
'defaults' => array(
'controller' => 'messages-controller-index',
'action' => 'index'
)
),
'may_terminate' => true,
'child_routes' => array(
'types' => array(
'type' => 'literal',
'options' => array(
'route' => '/types',
'defaults' => array(
'action' => 'types'
)
)
)
)
)
)
)
Module Tasks
'controllers' => array(
'invokables' => array(
'tasks-controller-index' => 'Tasks\Controller\IndexController',
)
),
'router' => array(
'routes' => array(
'tasks' => array(
'type' => 'literal',
'options' => array(
'route' => '/tasks',
'defaults' => array(
'controller' => 'tasks-controller-index',
'action' => 'index'
)
),
'may_terminate' => true,
'child_routes' => array(
'types' => array(
'type' => 'literal',
'options' => array(
'route' => '/types',
'defaults' => array(
'action' => 'types'
)
)
)
)
)
)
)
So what's happening there is:
if route is /messages if routes to controller-alias messages-controller-index with indexAction()
if route is /messages/types it stays at controller-alias messages-controller-index but goes to typesAction()
if route is /tasks if routes to controller-alias tasks-controller-index with indexAction()
if route is /tasks/types it stays at controller-alias tasks-controller-index but goes to typesAction()
You could obviously change controller aliases and meanings behind that. If you'd want to add an ID for a route like /messages/types/1 you'd build a child-route to the types-route that is of type segment and check for an [:id] parameter whose constraints should be numeric :) Check the official ZF2 Manual for more information, i'm lazy now :P
tl;dr: Make sure you have a __NAMESPACE__ default in your route definition, and then make sure all your service names are prefixed with it. Usually, the value will match your module namespace.
For the longer explanation:
I'm going to be blunt: it's a really bad practice to have a dynamic segment in your route that maps to the controller (:controller in your example). The reason is that if somebody discovers this, they can do controller injection, and request a controller that should not be routed by this particular route.
Additionally, the practice can easily lead to collisions, particularly if you omit a __NAMESPACE__ default in your route configuration -- which, based on the question, you've already discovered.
I am getting strange error. please help
I am creating custom module named "Advinvite"
I have created table : "engine4_advinvite_matchinvites"
I have created DbTable : "Advinvite_Model_DbTable_Matchinvites"
I have created DbTable's Row class model : "Advinvite_Model_Matchinvite" And Primary key in the table is "matchinvite_id"
I have also created Api: "Advinvite_Api_Core"
The main part I have also registered the item in manifest.php file of settings folder of module like :
// Items ---------------------------------------------------------------------
'items' => array(
'advinvite_matchinvite',
)
Now regarding Error :
I am getting error "Unknown Item Type : advinvite_matchinvite" when i want to use
Engine:Api:_()->getItem('advinvite_matchinvite',its_Identity)
I am sending the manifest file code too below. please can you check it for me and help me. It has been come in my life first time (weird error) and i am trying to solve this for last three/four days. Even Everything i did correctly why it is happening Not understanding. Please help me mate. It will be very appreciated.
And also if you can help me with this too means Routes are also not working....Don't know why these both errors are coming
Help Me Please.
Manifest.php Code:
<?php return array (
'package' =>
array (
'type' => 'module',
'name' => 'advinvite',
'version' => '4.0.0',
'path' => 'application/modules/Advinvite',
'title' => 'Advanced Invites',
'description' => 'This will invite the users for matches and nets',
'author' => 'Gitesh Dang',
'callback' =>
array (
'class' => 'Engine_Package_Installer_Module',
),
'actions' =>
array (
0 => 'install',
1 => 'upgrade',
2 => 'refresh',
3 => 'enable',
4 => 'disable',
),
'directories' =>
array (
0 => 'application/modules/Advinvite',
),
'files' =>
array (
0 => 'application/languages/en/advinvite.csv',
),
// Items ---------------------------------------------------------------------
'items' => array(
'advinvite_matchinvite',
),
// Routes --------------------------------------------------------------------
'routes' => array(
'advinvite_general' => array(
'route' => 'invites/:action/*',
'defaults' => array(
'module' => 'advinvite',
'controller' => 'index',
'action' => '(match|matchview)',
),
'reqs' => array(
'action' => '\D+',
)
),
)
),
); ?>
Waiting for postive response.
Kind regards
Mates,
It has been fixed.
The issue was minor :)
Items was registered in package array and it should be registered outside of package array. Package array should be closed to files array only.
Corrected Manifest File :
<?php return array (
'package' =>
array (
'type' => 'module',
'name' => 'advinvite',
'version' => '4.0.0',
'path' => 'application/modules/Advinvite',
'title' => 'Advanced Invites',
'description' => 'This will invite the users for matches and nets',
'author' => 'Gitesh Dang',
'callback' =>
array (
'class' => 'Engine_Package_Installer_Module',
),
'actions' =>
array (
0 => 'install',
1 => 'upgrade',
2 => 'refresh',
3 => 'enable',
4 => 'disable',
),
'directories' =>
array (
0 => 'application/modules/Advinvite',
),
'files' =>
array (
0 => 'application/languages/en/advinvite.csv',
),
),
// Items ---------------------------------------------------------------------
'items' => array(
'advinvite_matchinvite',
),
// Routes --------------------------------------------------------------------
'routes' => array(
'advinvite_general' => array(
'route' => 'invites/:action/*',
'defaults' => array(
'module' => 'advinvite',
'controller' => 'index',
'action' => '(match|matchview)',
),
'reqs' => array(
'action' => '\D+',
)
),
)
); ?>
Cheers !! :)