Yii url manager only ID in url - 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']

Related

CakePHP 3 routing dynamic url

I have an eCommerce website and need to create some custom URLs. These URLs will be generated dynamically. The structure will be
Example URL: http://website.com/categoryname/product-title
Here the category name is dynamic. It will change for each product, it can be
website.com/buttons/CP1 or
website.com/ribbons/red so on
In any case these URLs should be redirected to http://website.com/products/productdetail/product-title
I tried the following script:
$routes->connect(
'/:type/:slug/*',
['controller' => 'Products','action' => 'productDetail'],
['type' => '\b(?:(?!admin)\w)+\b'],
[
'slug' => '[A-Za-z-]+',
'pass' => [
'slug'
]
]
);
['type' => '\b(?:(?!admin)\w)+\b'] is to exclude the other controllers. In this case 'Admin'. This works but the parameters passed through the URL (product-title) is not getting in the controller function(productDetail)
Any help will be appreciated.
It is fixed. Updated script below.
$routes->connect(
'/:type/:slug/*',
['controller' => 'Products','action' => 'productDetail'],
[
'type' => '\b(?:(?!admin)\w)+\b',
'pass' => ['slug']
]);
According to the API the connect() function only accepts 3 parameters and you are actually passing 4. See Cake Routing Router - Connect
I have not tested this at all, but I think you should re-write it like this:
$routes->connect(
'/:type/:slug/*',
['controller' => 'Products','action' => 'productDetail'],
[
'type' => '\b(?:(?!admin)\w)+\b',
'slug' => '[A-Za-z-]+',
'pass' => ['slug']
]
);

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 BjyAuthorize access control from database?

Hi i set up zend framework 2 + BjyAuthorize + ZfcUser running, now i need some help.
Where to customize user register to chose roles?
Make all controllers under / route public and /admin for authenticated users?
How to configure access control for controller / action under /admin route on database?
I don't understand what you want to do.
Looking at the examples for the Route Guard at https://github.com/bjyoungblood/BjyAuthorize it does not seem like you can use wildcards. I'd use the Controller Guard and set permissions so that guests, users and admins can access everything apart from whatever controller(s) are used in the admin section.
'guards' => array(
'BjyAuthorize\Guard\Controller' => array(
array('controller' => 'admin', 'roles' => array('admin')),
array(
'controller' => array('index', 'anothercontroller', 'yetanothercontroller', ...),
'roles' => array('guest','user')),
)
Instead of array('controller' => 'admin', 'roles' => array('admin')),, set rules for each action. For example, assuming "founder" and "moderator" are sub-roles of "admin":
array(
'controller' => 'admin',
'action' => array('addUser', 'deleteUser'),
'roles' => array('founder')),
array(
'controller' => 'admin',
'action' => array('deleteComment'),
'roles' => array('moderator')),

Url routing for custom controller in yii

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>',

Zend Framework 2 - BjyAuthorize always denies access

I have setup the bjyoungblood/bjy-authorize module, but I am currently getting a 403 "access denied" error for each URL except for the one configured in the home route.
My module.byjauthorize.global.php looks like following:
'bjyauthorize' => array(
'guards' => array(
'BjyAuthorize\Guard\Controller' => array(
array('controller' => 'index', 'action' => 'index', 'roles' => array('guest','user')),
array('controller' => 'index', 'action' => 'stuff', 'roles' => array('user')),
array('controller' => 'zfcuser', 'roles' => array()),
//backend
array('controller' => 'Application\Controller\Index', 'roles' => array('admin')),
array('controller' => 'MyModule\MyEntity\MyEntity', 'roles' => array('admin')),
),
'BjyAuthorize\Guard\Route' => array(
array('route' => 'zfcuser', 'roles' => array('user')),
array('route' => 'zfcuser/logout', 'roles' => array('user')),
array('route' => 'zfcuser/login', 'roles' => array('guest')),
array('route' => 'zfcuser/register', 'roles' => array('guest')),
array('route' => 'home', 'roles' => array('admin')),
array('route' => 'my-entity', 'roles' => array('admin')),
),
),
),
I tried deleting the BjyAuthorize\Guard\Route part, but with no effect.
When I remove the home route then the homepage is also blocked.
So both Controller- and Route-Guard seem to work.
How can I debug this behavior?
NOTE: following is valid for BjyAuthorize 1.2.*
First of all, consider that protecting both the routes and the controllers is unnecessary. I personally always protect the controllers only, since there may be multiple routes to a same controller.
Once you removed either the route or the controller guard's config, you can:
Install Zend Developer Tools, which allows you to have an overview of the currently set Acl role, like in this picture:
Check if you have configured the correct identity provider: the default one uses ZfcUser's user id and looks up his role in the user_role table.
Check that the guest role has access to the public pages, such as the zfcuser controller (for login actions) or the zfcuser/login route.
As Akrabat pointed out, the configuration for the BjyAuthorize\Guard\Controller and BjyAuthorize\Guard\Route are whitelists, which basically means that you have to setup access for the default guest role if you want to browse pages being un-authenticated.
As soon as a guard is configured, it blocks access to any not configured resource, so be sure that you have granted the role guest (or whatever you configured in $config['bjyauthorize']['default_role'] access at least the login controller or route.
As soon as you create one entry in the 'BjyAuthorize\Guard\Controller' array, then you need to create entries for every controller with permissions as appropriate.
I have this:
'BjyAuthorize\Guard\Controller' => array(
// Access for everyone
array('controller' => 'zfcuser', 'roles' => array('guest')),
array('controller' => 'Application\Controller\Index', 'action' => 'index', 'roles' => array('guest')),
array('controller' => 'error', 'roles' => array('guest')),
// Restricted
array('controller' => 'User\Controller\AdminUser', 'roles' => array('admin')),
),
It's important that you give guest access to zfuser (for logging in!) and error (hard to debug stuff otherwise).
I've not tried using controller and route guards simultaneously.
I had the exact same issue.
I think the problem is that BjyAuthorize is not well documented so many of us are simply copying and pasting and working out from the files provided. For instance from the following:
'BjyAuthorize\Guard\Controller' => array(
array('controller' => 'zfcuser', 'roles' => array()),
),
You would expect to add your controllers as such:
array('controller' => 'controllername', 'role' => array()),
However you need to add the full path otherwise it will not work:
array('controller' => 'Folder/Controller/Action', 'role' => array()),
I hope this saves someone a few hours work as I was totally befuddled by this!
debug your code by this in module.php
public function onBootstrap($e)
{ echo "<pre>";
var_dump($e->getTarget()->getServiceManager()->get('BjyAuthorize\Provider\Identity\ProviderInterface'));
}

Resources