How to run controller function on every request? - php

I'm using codeigniter and have a controller Checkme.php and function inside talkcheck. Via url i can access it like http://site.com/checkme/talkcheck
I want this to run every time when codeigniter is loaded ( page requested ), how would I go with this?
I tried solution from here but it overwrittes exiting object and half of my methods don't work

You can use a hook to run a function on every page load by setting enable_hooks to TRUE in config.php and adding the following code to config/hooks.php
$hook['pre_controller'] = array(
'class' => 'Checkme',
'function' => 'talkcheck',
'filename' => 'Checkme.php',
'filepath' => 'controllers'
);

Related

In Codeigniter how to make library wait until view is rendered

I want to show a modal when triggered by my custom class library, but without explicity calling any of its methods from outside.
I would have to wait until view template is loaded.
How can I make my library's methods wait until view is loaded?
As first step I have already added my library to the autoload array.
Did you try Hooks?
Following may work but not sure. Didn't try myself.
$hook['post_controller'] = array(
'class' => 'MyClass',
'function' => 'Myfunction',
'filename' => 'Myclass.php',
'filepath' => 'hooks',
'params' => array('beer', 'wine', 'snacks')
);
Reference - https://codeigniter.com/userguide3/general/hooks.html

CakePHP loginAction not working

What I want:
When a user is not logged in and tries to visit the site at any url they get redirected to /landing (which has a form on it that posts to /login)
What is happening:
When a user is not logged in and tries to visit the site at any url they get redirected to /login
In my AppController I have:
public $components = array(
'Auth' => array(
...
'loginRedirect' => array('controller' => 'twitter', 'action' => 'index'),
'loginAction' => '/landing',
'logoutRedirect' => '/landing',
'unauthorizedRedirect' => '/landing',
'authorize' => array('Actions' => array('actionPath' => 'controllers'))
));
public function beforeFilter() {
$this->Auth->allow('login', 'landing');
}
I thought 'loginAction' => '/landing' would have been the solution but it has not helped
Any ideas?
You are missing the controller?
'Auth' => array(
'loginRedirect' => array(
'controller' => 'items',
'action' => 'index'
),
'logoutRedirect' => array(
'controller' => 'users',
'action' => 'login'
),
'authenticate' => array(
'Form' => array(
'passwordHasher' => 'Blowfish'
)
)
)
From your snippet it looks like you're putting $this->Auth->allow() rules in the beforeFilter() method of AppController:
public function beforeFilter() {
$this->Auth->allow('login', 'landing');
}
The problem is that since your /landing route points to your pages controller and /login to your users controller, unless your controllers' beforeFilter() method call their parent's method like this:
public function beforeFilter() {
parent::beforeFilter();
}
these rules won't ever take action. And if you add this code above, you're actually allowing both landing and login methods in both controllers (if they exist).
The way it is now, the parent is (apparently) never called, and so when you visit a url like /bogus, you get redirected to /landing as you've set in AuthComponent's loginAction. But since this is never explicitely allowed in pages controller beforeFilter(), you get redirected again to /login. The whole process looks like you are always redirected to /login no matter what url you visit.
Keep in mind, that as you have it, the allow() rule for login is neither executed. You're able to visit /login though, because cake automatically allows it (or else you would end up in an endless loop).
So first remove the landing rule from allow(). You don't need it.
Then move the beforeFilter() code to the pages controller. Change landing to display as the method that is called is display inside that controller, being passed the argument landing. There's no landing method there (I assume - and if there is, the route is pointing to pages' display so it wouldn't have any effect)!
This last change will make display action allowed for any given page passed as argument (like landing, home, eula or whatever else you might be having). If you don't want that you should add a check either in the beforeFilter() or the display() and only allow the request to continue if the argument is landing.

Unable to unset Phalcon aliases

I am using INVO example app from here:
https://github.com/phalcon/invo
I have copied all the files and set the db and base url.
It works, I can login etc.
however, I wanted to learn how to use redirects e.g.
I would like to use contact-us instead of contact without changing the name of the controller.
So, I created a file routes.php inside of the app/config folder and put this inside:
<?php
$router = new Phalcon\Mvc\Router(false);
$router->add('/contact-us', array(
'controller' => "contact",
'action' => "index"
))->setName('contact');
$router->handle();
return $router;
and I have created this in my bootstrap file index.php in the public root directory
$di = new \Phalcon\DI\FactoryDefault();
$di->set('router', function(){
require __DIR__.'/../app/config/routes.php';
return $router;
});
However, it's not working and when I try to access http://localhost/test/contact-us it works, but http://localhost/test/contact-us stopped working and I am redirected to the homepage.
If I (comment the route) do this:
/*$router->add('/contact-us', array(
'controller' => "contact",
'action' => "index"
))->setName('contact');*/
$router->handle();
return $router;
Neither http://localhost/test/contact-us nor http://localhost/test/contact-us works ;(.
If I uncomment it back. contact-us works but contact don't.
I guess it's b/c of ->setName('contact') and it's stored in the memory or in some file.
how to get it back to the original state and "unset" that?
You can create two routes and each will work:
$router->add('/contact', array(
'controller' => "contact",
'action' => "index"
));
$router->add('/contact-us', array(
'controller' => "contact",
'action' => "index"
));
Or use groups of routes.
So, if you have ContactController and don't want to access it using /contact url you should add router to the DI container. After that the pages will be available only via router and default /controller/action request format will not work.

Unable to route with module in ZF2

I've created a module, a basic copy of the the albums example given in the ZF2 documentation, however, with the new module, I am not able to access it at all - I'm always given a 404 error. I'm building this on the ZF2 skeleton.
I've got three modules loaded: Application, Frontend and Security.
Both Frontend and Security are duplicates of each other, however, I have thoroughly checked and there is no reference to old code (as I literally copied the module folder and renamed/rewrote references).
The module is also loaded in application.config.php.
Any ideas on what I'm missing?
Module Config:
return array(
'controllers' => array(
'invokables' => array(
'Security\Controller\Security' => 'Security\Controller\SecurityController',
),
),
'router' => array(
'routes' => array(
'security' => array(
'type' => 'segment',
'options' => array(
'route' => '/security[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Security\Controller\Security',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'security' => __DIR__ . '/../view',
),
),
);
I had the same problem while following the skeleton application tutorial (Getting started: A skeleton application). Whenever I would go to the album url in the browser (ZendSkeletonApplication/public/album in my case), I would get a 404 error page but no details on why I got the 404. It wasn't clear to me how I would be able determine why I was getting the 404 when I had double checked everything and was pretty sure I copied and configured the Album module properly. It turned out that I was missing a slash in my route (module.config.php). For example I had 'route' => 'album[/:action][/:id]' instead of 'route' => '/album[/:action][/:id]'.
I was only able to figure it out by intentionally causing errors by misspelling things like making the 'Album\Controller\Albums' instead of 'Album\Controller\Album'in the invokables value, this would cause a stack trace to display which then showed the ZF2 classes that where called on the request. I would continue to misspell, test, and then correct each part of the module.config.php until I was given a clue to what part of the configuration was causing the error.
I'm pretty sure this was not the best way to debug an application's configuration.
There is few things that need to be make sure is:-
You have to add your module in
application.config.php (which you are saying you done it.)
Security\Controller\Security has to be same in default too (which you already has)
One more thing is Your folder structure....
-
Just to doulbe check you have a /MODULE/src/MODULE/Controller/CONTROLLER_FILE_NAME.php
I hope that helps..
I know it is an old post. However another thing to make sure you have in the modules top directory (same directory as the Module.php file) is the "autoload_classmap.php"
file with "<?php return array();?>" inside of it.
A simple tip to know whether your rule has already added correctly to the routes or not, you may check the routes value in the config file inside any working module, as following:
$config = $this->serviceLocator->get('config');
var_dump($config);

extension question,YiiFramework

I use the module user
http://yiiframework.com/extension/yii-user/
then open localhost/testdrive and get an error - trying to get property of non-object
on line - array('url'=>Yii::app()->getModule('user')->loginUrl,
did everything according to instructions from the link
Maybe you should add 'class' parameter to 'user' to configuration in main.php, ie:
'components'=>array(
'user' => array(
'class' => 'application.components.WebUser',
'allowAutoLogin' => true,
'loginUrl' => array('/ua/user/login'),
)...
I figured out why this was happening to me, too. I had put the block at the end of main.php. If you put it at the beginning, the problem goes away.

Categories