Url routing for custom controller in yii - url

I'm new in yii framework and have a problem with url routing.
I have one controller - StaticPage and actions index (default) and send.
Thats my config:
'urlManager' => array(
'showScriptName' => false,
'urlFormat' => 'path',
'rules' => array(
'call' => 'staticPage/index',
'call/send' => 'staticPage/send'
),
),
When i try set pattern like this 'call/<_a>' => 'staticPage/<_a>' i get 404 error, why ?

Always put the more specific rules on top. After a rule matches all following rules will not be checked anymore. That means in your case, if you try the URL /call/send, the first rule will match and route to staticPage/index.
If you want to add 'call/<_a>' => 'staticPage/<_a>' make this the first rule and remove the 'call/send' rule.

This works for me:
'call' => 'staticPage/index',
'call/<action:\w+>' => 'staticPage/<action>',
//or 'call/<action:(send|abc|something)>' => 'staticPage/<action>',

Related

ZF2 - Formatting Routes

I typically format my routes in ZF2 like so: /name/to/route
Now I have been doing the same thing with my api routes however I am finding that I am struggling to include data such as encoded urls or arrays.
Here is an example of such a route:
http://example.com/api/register/access/code/c102dea422fa4bb6958d77a29d9873d2/http%3A%2F%2Frouter-local.example.com%2Fapi%2Fdirectory
The following represents forward slashes and thus causes the route not to work: %3A%2F%2
I am thinking I should encode my route as such:
http://example.com/api/register/access/code/?access_code=c102dea422fa4bb6958d77a29d9873d2&route=http%3A%2F%example.com%2Fapi%2Fdirectory
How do you configure the module.config file to deal with this?
Currently it is set as such in apigility:
'api.rpc.register-access-code' => array(
'type' => 'Segment',
'options' => array(
'route' => '/api/register/access/code/:access_code/:route',
'defaults' => array(
'controller' => 'Api\\V1\\Rpc\\RegisterAccessCode\\Controller',
'action' => 'registerAccessCode',
),
),
),
EDIT
I have encoded my routes to include GET parameters by doing the following:
$url = "http://example.com/api/register/access/code/";
$params = [
'access_code' => 'c102dea422fa4bb6958d77a29d9873d2',
'route' => 'http://example.com/api/directory'
];
$final = $url . "?" . http_build_query($params);
Which gives this:
http://example.com/api/register/access/code/?access_code=c102dea422fa4bb6958d77a29d9873d2&route=http%3A%2F%2Fexample.com%2Fapi%2Fdirectory
However this breaks due to a "The requested URL could not be matched by routing." error.
The route is unidentified due to the interpretation of the slashes in the included URL.
Perhaps the issue is to do with how the URL is formatted and included as a parameter?
You don't define query variables in the segment route option; only the path.
You may append ?query=vars to any url, regardless of route configuration. ZF2's url helpers should encode the query vars for you, you just have to create an array of query vars and give it to the helper function when creating a url.
<?php echo $this->url('api.rpc.register-access-code', array(), array('query' => array(
'access_code' => 'c102dea422fa4bb6958d77a29d9873d2',
'route' => 'http://router-local.example.com/api/directory',
))); ?>`
In this case, it would seem the problem is to do with htaccess or apache. The simplest solution has been to encode the url using: base64_encode($url) which can be de-coded at the other end.

Yii url manager only ID in url

i have in YII for example domain: www.example.com/product-url-1234
how to set rules in urlManager and how to use controller/action/id
If you want to use this url www.example.eu/product-url-1234
and suppose 'index' is the name of the action of the 'user' controller that will handle the request
Then create a rule like
'<id:.*?>'=>'user/index'
Now if you will use Yii::app()->createUrl('user/index',array('id'=>'product-url-1234'))
then it will give you the desired result.
And if you Visit this Url, User/Index will handle the request.
You are trying forward all request to one controller at root level.
I Assumed that all your request are redirected product/view route
in your config go to URL section,
'(.*)'=>'product/view'
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'rules' => array(
'(.*)' => 'product/view',
'post/<id:\d+>/<title:.*?>' => 'post/view',
'posts/<tag:.*?>' => 'post/index',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
that means you are capturing all your root requests to product controllers view action
Where you can get www.example.eu/product-url-1234 using
Yii::app()->request->requestUri in product/view
but this is not good way to capture all your incoming request to a single controller, the better way would be as follows.
www.example.eu/product/product-url-1234
then you have to change the configuration
'product/<product:.*?>'=>'product/view'
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'rules' => array(
'product/<product:.*?>' => 'product/view',
'post/<id:\d+>/<title:.*?>' => 'post/view',
'posts/<tag:.*?>' => 'post/index',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
in your view action, you can get the url through $_GET['product']

Adding parameters to URL in ZF2

I am trying to construct url which looks like this:
abc.com/folder?user_id=1&category=v
Followed the suggestion given in this link:
How can you add query parameters in the ZF2 url view helper
Initially, it throws up this error
Query route deprecated as of ZF 2.1.4; use the "query" option of the HTTP router\'s assembling method instead
Following suggestion, I used similar to
$name = 'index/article';
$params = ['article_id' => $articleId];
$options = [
'query' => ['param' => 'value'],
];
$this->url($name, $params, $options);
Now, I am getting syntax error saying,
Parse error: syntax error, unexpected '[' in /var/www/test/module/Dashboard/view/dashboard/dashboard/product.phtml on line 3
My module.config.php is configured like this:
'browse_test_case' => array(
'type' => 'Literal',
'options' => array(
'route' => '/browse-case[/:productId]',
'defaults' => array(
'controller' => 'Test\Controller\Browse',
'action' => 'browse-test-case',
),
),
'may_terminate' => true,
'child_routes' => array(
'query' => array(
'type' => 'Query',
),
),
),
Any Idea, please help!
Your url view helper call is wrong. First parameter is the name of a defined route, second parameter is an array with your route parameters. Your array syntax should also be correct. e.g.
array("param1" => "value1", "param2" => "value2")
In your example the correct url view helper call should be something like this:
$this->url('browse_test_case', array('productId' => '1'));
...in which "1" could be an database table row identifier, for example.
The Query child-route in your route definition, allows you to use also other url paramaters, than specified within the route. But each of these child-routes start with "/browse-case[/:productId]".
There you find the reference example of ZF2:
http://framework.zend.com/manual/2.3/en/modules/zend.view.helpers.url.html#zend-view-helpers-initial-url

zf2 website with a single entry-point, no routes/paths in URL

Is it be possible to make a website that doesn't reveal any relative URL's at all?
Say for example, I have a domain name "somedomain.xyz" and I want to route everything through the default route, and I want not to reveal any paths or route structures to the end user.
The end user shall only see the domain name in the browser's address bar, like:
http://somedomain.xyz
or
https://somedomain.xyz.
Any path like
http://somedomain.xyz/index.php
or
http://somedomain.xyz/index or
http://somedomain.xyz/index/index
shall show a 404.
And I don't care about SEO stuff and static pages.
Is that possible with ZF2, and if yes, then how?
similar question: hide module and action name from zf2 routing
Just create a hostname route for subdomain.xyz like so:
'my-route' => array(
'type' => 'Hostname',
'options' => array(
'route' => 'subdomain.xyz',
'defaults' => array(
'controller' => 'MyApp\Controller\TheController',
'action' => 'whatever-action',
),
),
),
see here for a complete solution, with using HTTP POST vars for the routing:
ZF2 routing via post vars

How to handle GET params within urlManager's rule in Yii?

I pass a query string to SearchController::actionDefault in form of GET parameter q:
/search/?q=...
However I need to define a rule that would automatically initialize this parameter with some value or define another param.
If I'll request mysite.com/showall I need get the same content like in /search/?q=*
This is what I've tried:
'/showall' => '/search/default/index/?r=*',
I solved this!
there is possible to set defaultParams in urlManager, and finaly it looks like in application config file:
...
'components' => array(
...
'urlManager' => array(
...
'rules' => array(
....
'/show_all' => array( '/search/default/index', 'defaultParams' => array('show_all'=>'-') ),
....
),
...
),
...
),
The accepted answer also works when you are getting different requests and you need to map it to the same GET param.
For example I want all of these requests:
user/pics
user/photos
user/pictures
to actually generate: user/index?content=photos.
This might be one of a way to go:
'<controller:user>/(<content:photos>|pics|pictures)' => array('<controller>/index', 'defaultParams'=>array('content'=>'photos')),

Resources