Zend Cache Front end - php

I'm using this function in Bootstrap.php to cache my controllers , and i need to cache some controllers only I don't need to cache index controller and article controller as an example
and i need to cache question controller but it does not working . this function is cached all my controllers
protected function _initCache()
{
mb_internal_encoding("UTF-8");
$dir = "/var/www/data/cache/all";
$frontendOptions = array(
'lifetime' => 3600,
'content_type_memorization' => true,
'default_options' => array(
'cache' => true,
'cache_with_get_variables' => true,
'cache_with_post_variables' => true,
'cache_with_session_variables' => true,
'cache_with_cookie_variables' => true,
),
'regexps' => array(
'^/$' => array('cache' => false),
"^/question/" => array('cache' => true),
"^/article/" => array('cache' => false),
)
);
$backendOptions = array(
'cache_dir' =>$dir
);
// getting a Zend_Cache_Frontend_Page object
$cache = Zend_Cache::factory('Page',
'File',
$frontendOptions,
$backendOptions);
$cache->start();
}
so what i can do I tried all soluations please help me.
Thanks

Create a controller plugin with that code, and detect the request.
Something like...
if($request->getControllerName() == 'index' || ... == 'article') {
return;
}
mb_internal_encoding("UTF-8");
...

Related

Silex Firewall setup for admin path

i need to setup my silex firewall like:
www.mysite.com/* => access to all users
www.mysite.com/admin/* => access to only logged in users
i use this set up but it does not work as expected:
$app->register(new SecurityServiceProvider(), array(
'security.firewalls' => array(
'secure' => [
'pattern' => '^/.*$',
'anonymous' => true,
'form' => array(
'login_path' => '/admin/login',
'check_path' => '/admin/auth'
),
'logout' => array(
'logout_path' => '/admin/logout'
),
'users' => $app->share(function() use ($app) {
return new AuthenticationSuccessHandler($app['db']);
}),
]
),
'security.access_rules' => array(
array('^/admin$', 'ROLE_ADMIN')
)
));
Any help?
Many Thanks!! ;-)
'users' => $app->share(function() use ($app) {
return new AuthenticationSuccessHandler($app['db']);
}),
The above function needs to return an object which implements
Symfony\Component\Security\Core\User\UserProviderInterface
Check here for custom user provider documentation
It may also be appropriate to move login_path outside the secured area. Another way of configuring would be:
$app['security.firewalls'] = array(
'secure' => array(
'pattern' => '^/admin/',
'form' => array('login_path' => '/login', 'check_path' => '/admin/auth'),
'users' => $app->share(function () use ($app) {
return new MyUserProvider($app['db']);
}),
),
),
);
$app['security.access_rules'] = array(
array('^/admin', 'ROLE_ADMIN')
);
Make sure you register doctrine dbal.

Silex Security Provider - Token failing to be set

I'm working for the first time with Silex's Security Provider and I'm having issues with the process. I currently have the basic HTTP auth working (using the coded example user as shown here in the docs).
When switching HTTP out for the form option however the login form is submitting, and returning to itself. I have created a UserProvider class and the loadUserByUsername method is being successfully called, however the email isn't being passed in (being set to "NONE_PROVIDED" - altered from username). This I found when working through the vendor code is because the token isn't being set ($app['security']->getToken() returning null at all points). I've trawled through all the docs I can but I can't find any mention of this.
The main code is included below, let me know if there is anything else, thanks!
Security Provider Configuration
// Protects all routes within /auth, redirecting to /login successfully
$app->register(new SecurityServiceProvider(), array(
'security.firewalls' => array(
'unauth_area' => array(
'pattern' => '^/(?!auth)'
),
'auth_area' => array(
'pattern' => '^/.*$',
'form' => array(
'login_path' => '/login',
'check_path' => '/auth/login_check',
'default_target_path' => '/auth/overview',
),
'users' => $app->share(function () use ($app) {
return new UserProvider($app['db']);
}),
),
),
'access_control' => array(
array('path' => '^/.*$', 'role' => 'ROLE_USER'),
// Include the following line to also secure the /admin path itself
// array('path' => '^/admin$', 'role' => 'ROLE_ADMIN'),
),
));
(My Custom) method - UserProvider class
public function loadUserByUsername($email) {
// Dying at this point shows it reaches here, but $email is null
$stmt = $this->conn->executeQuery('SELECT * FROM user WHERE email = ?', array(strtolower($email)));
if (!$user = $stmt->fetch()) {
throw new UsernameNotFoundException(sprintf('Email "%s" does not exist.', $email));
}
return new User($user['email'], $user['password'], explode(',', $user['roles']), true, true, true, true);
}
Form Class
class LoginType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('_username', 'text', array(
'required' => true,
'constraints' => array(
new Assert\NotBlank(),
new Assert\Email(),
)
))
->add('_password', 'password', array(
'required' => true,
'constraints' => array(
new Assert\NotBlank(),
),
))
->add('Login', 'submit');
}
public function getName() {
return 'login';
}
}
Silex Security Provider docs
It has nothing to do with the token… I just had the same problem with
$app->register(new Silex\Provider\SecurityServiceProvider(), array(
'security.firewalls' => array(
'admin' => array(
'pattern' => '^/admin',
'form' => array(
'login_path' => '/',
'check_path' => '/admin/login_check',
'username_parameter'=> 'mail',
'password_parameter' => 'password',
),
'logout' => array('logout_path' => '/logout'),
//'anonymous' => true,
'users' => function () use ($app) {
return new UserProvider($app['db']);
},
)
),
'security.access_rules' => array(
array('^/$', 'IS_AUTHENTICATED_ANONYMOUSLY'),
array('^/admin', 'ROLE_USER')
)
));
After a couple hours trying and testing, I checked the name attribute in my form's input… Saw form[mail]
So I tried
'username_parameter'=> 'form[mail]',
'password_parameter' => 'form[password]',
And … ALLELUIA!!!!! had my mail in loadUserByUsername($mail)

How do I To do the upload multiple files using Zend\InputFilter\Factory?

I'm using Zend Framework 2.2.5 at work.
I'm having problems certain problem occurs.
prepare for the following controllers.
public function indexAction()
{
$form = new \Zend\Form\Form();
$form->add(array(
'type' => 'Zend\Form\Element\File',
'name' => 'file',
'attributes' => array(
'multiple' => TRUE,
)
));
/** #+ */
$factory = new \Zend\InputFilter\Factory();
$file = $factory->createInput(array(
'name' => 'file',
'filters' => array(
array(
'name' => 'Zend\Filter\File\RenameUpload',
'options' => array(
'target' => './data/tmpuploads/',
'overwrite' => TRUE,
'use_upload_name' => TRUE,
),
),
),
));
/** #- */
/** #+ */
// $file = new \Zend\InputFilter\FileInput('file');
// $file->getFilterChain()->attach(
// new \Zend\Filter\File\RenameUpload(array(
// 'target' => './data/tmpuploads/',
// 'overwrite' => TRUE,
// 'use_upload_name' => TRUE,
// )
// ));
/** #- */
$inputFilter = new \Zend\InputFilter\InputFilter();
$inputFilter->add($file);
$form->setInputFilter($inputFilter);
if ($this->getRequest()->isPost()) {
$form->setData(array_merge_recursive(
$this->getRequest()->getPost()->toArray(),
$this->getRequest()->getFiles()->toArray()
));
if ($form->isValid()) {
\Zend\Debug\Debug::dump($form->getData());
}
}
$form->setAttribute('method', 'post')
->setAttribute('action', $this->url()->fromRoute('examples'))
->prepare();
return new \Zend\View\Model\ViewModel(array('form' => $form));
}
The following warning appears.
Warning: Illegal offset type in isset or empty in /mnt/shared/zf2/vendor/zendframework/zendframework/library/Zend/Filter/File/RenameUpload.php on line 175
Warning: basename() expects parameter 1 to be string, array given in /mnt/shared/zf2/vendor/zendframework/zendframework/library/Zend/Filter/File/RenameUpload.php on line 262
Warning: file_exists() expects parameter 1 to be a valid path, array given in /mnt/shared/zf2/vendor/zendframework/zendframework/library/Zend/Filter/File/RenameUpload.php on line 180
The problem is to solve If you switch the comments, but I'd like to use the Zend\InputFilter\Factory.
How can i solve this issue? Why this Warning occurred?
I solved the problem in the following manner.
$factory = new \Zend\InputFilter\Factory();
$file = $factory->createInput(array(
'name' => 'file',
'type' => 'Zend\InputFilter\FileInput',
'filters' => array(
array(
'name' => 'Zend\Filter\File\RenameUpload',
'options' => array(
'target' => './data/tmpuploads/',
'overwrite' => TRUE,
'use_upload_name' => TRUE,
),
),
),
));

PHP try catch not working with Zend cache Exception

I have this code to start Zend Cache , and I set the try cache exception to cancel Zend Cache if an error accorded during the process, but it does not work and does not print the error inside catch Exception.
$frontendOptions = array(
'lifetime' => $cacheTime,
'content_type_memorization' => false,
'default_options' => array(
'cache' => true,
'make_id_with_cookie_variables' => false,
'make_id_with_session_variables' => false,
'cache_with_get_variables' => true,
'cache_with_post_variables' => true,
'cache_with_session_variables' => true,
'cache_with_files_variables' => true,
'cache_with_cookie_variables' => true,
'tags' => $cacheTag,
),
'regexps' => array(
'$' => array('cache' => true),
)
);
$backendOptions = array('cache_dir' => '/var');
$cache = Zend_Cache::factory('Page', 'File', $frontendOptions, $backendOptions);
try {
$cache->start();
} catch (Exception $exc) {
echo $exc->getMessage();
}
and I tried the $cache->cancel() without any result.
Thanks

init Zend_Cache in Bootstrap (UTF-8)

I'm tryng to Cashe controllers from Bootstrap
I have controller called questionController and I'm using Arabic language
when I cached the questionController (like code below) its work properly . but when I added routing to this controller like my.local/اسئله to questionController cache dose not work its only work when i call controller directly with his name but with his routing (Arabic) name dose not work.
my function function in my Bootstrap.php is
protected function _initCache()
{
mb_internal_encoding("UTF-8");
$dir = "/var/www/data/cache/";
$frontendOptions = array(
'lifetime' => 10800,
'automatic_serialization' => true,
'debug_header' => true,
'regexps' => array(
'$' => array('cache' => false),
'/question' => array('cache' => true),
),
'default_options' => array(
'cache_with_cookie_variables' => true,
'make_id_with_cookie_variables' => false
)
);
$backendOptions = array(
'cache_dir' =>$dir
);
$cache = Zend_Cache::factory('Page',
'File',
$frontendOptions,
$backendOptions);
$cache->start();
}
and my rout is
$router->addRoute('questionRout', new Zend_Controller_Router_Route('اسئله', array('controller' => 'question', 'action' => 'view')));
my.local/question ---> cache working
my.local/اسئله ---> not working
and I used mb_internal_encoding("UTF-8").
Please help me
Thanks
in your front end options for the cache you are isolating out the /question url slug to be included in the cache
'/question' => array('cache' => true),
so this will exclude other paths.

Categories