cakePHP prevent user enter URL - url

I use router
Router::connect(
'/articles/:id/:slug',
array('controller' => 'articles', 'action' => 'view'),
array(
'pass' => array('id', 'slug'),
'id' => '[0-9]+'
)
);
BUT how to prevent user enter /articles/view/:id .I can make page /articles/view/:id become " NOT FOUND " ?

Router::connect(
'/articles/view/:id',
array('controller' => 'articles', 'action' => 'index'),
);

you are passing the slug, so just check that $this->params['slug'] isset and if it is not, redirect them to the home page.

Related

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

How to tell the Zend validator to use the only one validation message in any way

I've added the EmailAddress validator to my form element and when I'm trying to submit something like test, it shows me 2 errors:
* The input does not match against pattern '/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+#[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/'
* The input is not a valid email address. Use the basic forma local-part#hostname
But I don't want to fright my users with such a terrible validation information. Of course I can redeclare the message for each validation error, but the perfect for me would be to set the only one error message like The input is not a valid email address. Use the basic forma local-part#hostname.
I can't believe that there is no way in zf2 to do that.
I found this code work for me. I hope this works for you too.
'validators' => array(
array (
'name' => 'Regex',
'options' => array(
'pattern'=>'/^[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]+#[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/',
'messages' => array(
Regex::NOT_MATCH => 'Please provide a valid email address.',
),
),
'break_chain_on_failure' => true
),
array(
'name' => 'EmailAddress',
'options' => array(
'messages' => array(
EmailAddress::INVALID_FORMAT => 'Please provide a valid email address.',
EmailAddress::DOT_ATOM => '',
EmailAddress::INVALID_FORMAT => '',
EmailAddress::INVALID_LOCAL_PART => '',
EmailAddress::QUOTED_STRING => '',
)
),
),
),

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'));
}

ZF2. if I have literal route - will it also catch a url with GET params or not?

I'm a bit confused about params in ZF2 routing. Here's the literal route:
'route-test1' => array(
'type' => 'literal',
'options' => array(
'route' => '/my/route',
'defaults' => array(
'controller' => 'IndexController',
'action' => 'myRoute',
),
),
),
Should that route catch only:
example.com/my/route
Or also:
example.com/my/route?test1=aaa&test2=bbb
example.com/my/route?test=aaa
RESOLUTION:
Tested id (dunno why I haven't done it int he first place) - and yes - it will match all of these urls - with or without query line.
Yes. Then you can access the arguments (from a controller, for example) with:
$this->getRequest()->getQuery()

Zend framework 2, pagination routing issue

I have issue on zend pagination and routing in zf2 . I would like to display details of feedback item , and list of its sub items ( actions ) on the same page . My route code is given below
$routes['dashboard_inbox_actions'] = array(
'type' => 'segment',
'options' => array(
'route' => '/dashboard/inbox/detail[/:feedback[/actions/page/:page]]',
'constraints' => array(
'feedback' => '[0-9]+',
'page' => '[0-9]+',
),
'defaults' => array(
'__NAMESPACE__' => 'Dashboard\Controller',
'controller' => 'inbox' ,
'action' => 'detail',
'feedback' => 0 ,
'page' => 1
),
),
);
I pass url like
/dashboard/inbox/detail/4
in listing page , for rendering the provided pages of subitems .
<?php echo $this->paginationControl($this->paginator, 'Sliding' ); ?>
which creates paging urls , with feedback id as 0 ( it my issue )
/dashboard/inbox/detail/0/actions/page/2
/dashboard/inbox/detail/0/actions/page/3
I manually paste url
/dashboard/inbox/detail/4/actions/page/2
Its shows page 2 as active item . My controller code works fine and gives me result , but still paginationControl creates url with feedback id 0.
You need to use the fourth parameter of the paginationControl view helper:
<?php
echo $this->paginationControl($this->paginator, 'Sliding',
'my_pagination_control', array('route' => 'paginator_route'));
?>
You can pass through parameters to the view partial, for example pass through your route name so you can generate yoru links using the correct route.
then inside your view partial you can use this in the url helper:
<?php echo $this->url($this->route, array('page' => $this->first), FALSE, TRUE) ?>
see: http://framework.zend.com/manual/2.0/en/modules/zend.view.helpers.html#url-helper
where you can see the url helper can use currently matches params:
url($name, $urlParams, $routeOptions, $reuseMatchedParams)
Setting $reuseMatchedParams to true will allow the use of the current matched params as default values.

Resources