I wrote an Auth plugin to check if the user is logged in. No unlogged user should be able to visit anything in the app except the login page.
So I have this in the file application/modules/user/plugins/Auth.php:
class User_Plugin_Auth extends Zend_Controller_Plugin_Abstract {
public function preDispatch(Zend_Controller_Request_Abstract $request) {
if (Zend_Auth::getInstance()->hasIdentity()
|| $this->getRequest()->getActionName() == 'login') return;
$request->setModuleName('user');
$request->setControllerName('auth');
$request->setActionName('login');
}
}
Then I made this in the application.ini:
pluginPaths.User_Plugin = APPLICATION_PATH "/modules/user/plugins/"
resources.frontController.plugins[] = "User_Plugin_Auth"
However, no matter how I move the Auth.php file and no matter the name, I always get Fatal error: Class 'User_Plugin_Auth' not found. Please help me, I have wasted more than one hour on this and it's frustrating.
I think the problem is to do with the filename. I would try creating copies of the files in these locations
application/modules/user/plugins/User_Plugin_Auth.php
application/modules/user/plugins/User/Plugin/Auth.php
naturally you only need one of them, so delete the others once you have found the one that works.
If that doesn't help that the syntax I have in my config (Fam is just the codename of the project)
resources.frontController.plugins.layout = "Fam\Controller\Plugin\Layout"
resources.frontController.plugins.route = "Fam\Controller\Plugin\Route"
as pointed out in comments that is php 5.3 - this should work in older versions assuming include paths are configured
resources.frontController.plugins.layout = "Fam_Controller_Plugin_Layout"
resources.frontController.plugins.route = "Fam_Controller_Plugin_Route"
which maps to the files in my library, eg
APPLICATION_PATH "/../library/Fam/Controller/Plugin/Layout.php"
For reference in this project my Zend files are in
APPLICATION_PATH "/../library/Zend"
so adjusting the files to be in the relative place should do the trick
is your autoloader configured? I have
autoloaderNamespaces.0 = "Fam"
you might need something like
autoloaderNamespaces[] = "User_"
resources.frontController.plugins.UserAuth = "User_Plugin_Auth"
Related
I've taken over an old CakePHP 2.X website and have no prior experience with CakePHP so please forgive me if this is a stupid question.
I was looking at making some changes to some vendor files and noticed that we appear to have multiple copies of various files (which are, for the most part, identical) in 2 different places:
app/webroot/api/vendor/API/lib/
vendors/API/lib/
Additionally I noticed there are several other vendor directories in various other places.
I am using App::import('Vendor', 'example', array('file' => 'API/lib/example.php')); to load the scripts in question.
Could someone please explain to me what the best practices are regarding file structure relating to vendor files? Additionally, am I safe to delete the duplicate copy of all the files? How does CakePHP know which copy to load?
Edit:
I have come to the conclusion that the files are being loaded from vendors/API/lib/ rather than app/webroot/api/vendor/API/lib/, is it possible that the files in the latter location are redundant? I cannot seem to find any references to them.
Well as Sudhir has commented you, there is a folder in your app project which is called Vendor. I would recommend you to put it there.
app > Vendor
For example, I have created a folder called Csv for generating my own csv files through a Shell which is launching them. It is located inside app > Vendor > Csv
For importing this to my projects, I did the next for being able to use it:
<?php
include('GenericShell.php');
require_once(ROOT . DS . 'app' . DS . 'Vendor' . DS . 'Csv' . DS .
'CsvGenerator.php');
class CsvPatientsShell extends GenericShell {
That's one only example with PHP.
One other one would be, if in this case you have a Component which is called component.php and you want to import it to a Controller which you use frequently inside your project :
Component would be located into
Controller > Component > Namecomponent.php
The next thing you would have to do would be to do the import likewise inside your controller:
Let's say your controller's name is NameController.php and is located inside the Controller folder.
Controller > NameController.php
public function main_function() {
App::import('Component', 'Namecomponent');
$NameComponent = new NameComponent();
$this->layout = null;
$this->autoLayout = false;
die();
}
That would be a more correct way to do it with CakePhp but both mentioned are legit I'd say.
I hope that will help you somehow.
I'm working on a new website. This website will be a one pager. All my files I already load in through PHP into the main folder. But now I want to edit them and update them through WYSIWYG.
The UPDATE and SELECT are already working. I tested it on a page who stood on its own. All the one-pager files are stored in a folder and within the folder is also the file which loads all the files into it. I call them through a href which ends up giving the file an # in the address bar. There lies the problem. I can't access the #file with the function I wrote because that only can access files without starting a #. Is it possible to access it through my function?
I give the code if the question is too unclear because it's a bit of mess to implement all the files I use for this purpose.
Short recap: Can't access #domain with a PHP function. Is it even possible to access it.
<?php
class Home extends Controller {
protected function frontpage() {
$viewmodel = new HomeModel();
$this->returnView($viewmodel->frontpage(), true);
}
}
?>
this is the returnView from Controller:
protected function returnView($viewmodel, $fullview){
$view = 'views/'. get_class($this). '/' . $this->action. '.php';
if($fullview){
require('views/main.php');
} else {
require($view);
}
}
I'm new with overflow so I couldn't get the function into right place but with protected function frontpage I should access the file frontpage.php. Well it does do that but that's not the right directory because it's only visible throug a href which means the function should have been: protected function #frontpage which isn't possible.
This is actually not possible, as the anchor (#) isn't sent to the server and is handled by the browser itself.
The only possibility is to include some JavaScript magic to your project.
I wrote custom classes and want to use them in pimcore application.
I took them to /website/lib/Custom directory on server. Afterwards, I wrote recursive script includer for each Class located in the directory and included that script in /index.php file.
It is absolutely not pimcore standard but it works.
In pimcore/config/startup.php exists snippet:
$autoloaderClassMapFiles = [
PIMCORE_CONFIGURATION_DIRECTORY . "/autoload-classmap.php",
PIMCORE_CUSTOM_CONFIGURATION_DIRECTORY . "/autoload-classmap.php",
PIMCORE_PATH . "/config/autoload-classmap.php",
];
$test = PIMCORE_ASSET_DIRECTORY;
foreach ($autoloaderClassMapFiles as $autoloaderClassMapFile) {
if (file_exists($autoloaderClassMapFile)) {
$classMapAutoLoader = new \Pimcore\Loader\ClassMapAutoloader([$autoloaderClassMapFile]);
$classMapAutoLoader->register();
break;
}
}
I guess that this provides inclusion of all those classes put into returning array from autoload-classmap.php.
Having in mind that /pimcore/config/autoload-classmap.php exists, the mentioned loop would break at first iteration so classes that I would put into custom autoload-classmap are not going to be included in project.
My question is can I change files from /pimcore directory and expect that everything would be fine after system update?
No, you should not overwrite anything in the pimcore directory, since the files in there get overwritten by the update mechanism.
You can do what you want by using the /website/config/startup.php which will not get overwritten:
https://www.pimcore.org/wiki/display/PIMCORE4/Hook+into+the+startup-process
But instead of loading all your classes as you did, take advantage of the autoloader by adding this to the /website/config/startup.php:
// The first line is not absolutely necessary, since the $autoloader variable already gets
// set in the /pimcore/config/startup.php, but it is a more future-proof option
$autoloader = \Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('Custom');
If you are properly using namespaces and naming your files correctly that's all you need to do.
I need to be able to "effectivly" redeclare my class, so that during runtime, whilst my PHP IRC bot is running I can reload modules as the code base changes, which requires getting the class, but PHP won't allow it to be redeclared, nor is there a way to undeclare a class.
Is there a way I can acheive this? Perhaps some other method? I've tried "runkit", but that failed.
My current code is here:
http://pastie.org/private/ptj7c0t0teh3nnzn7ehcg
So to clarify, I effecivly need to be able to reload my class, instatiate it and put into a property in my main bot class once code has changed in said module, whilst the bot is running (run-time).
A brief look at your application leads me to believe your best course of action is to use a base class as suggested in the comment above.
Something to the extent of:
class IRCModule {
private static $module_dir = ''; //put your module directory here w/o trailing slash
public static function getModule( $module ) {
//if the module directory doesn't exist, don't do anything
if( !is_dir( self::$module_dir.DIRECTORY_SEPARATOR.$module ) ) return false;
//load the module file
$fname = scandir(self::$module_dir.DIRECTORY_SEPARATOR.$module);
$fname = $fname[2]; //first 2 elements will be . and ..
require_once( self::$module_dir.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$fname );
$className = str_replace('.class.php',NULL,$fname);
return new $className();
}
}
You would then extend that using your modules. That would allow you to overwrite a module by simply removing it's old file /my/module/dir/moduleName/moduleNameV1.0.class.php and replacing it with a new version /my/module/dir/moduleName/moduleNameV1.1.class.php
As mentioned in the comments, this will eventually fill the memory on the server, so you should schedule a reboot of the service each time you make substantial changes, but it also allows you to load new versions on demand without stopping the service.
A more stable approach would be to take advantage of process control and spin off daemons for each connection from your parent script, or implement a cache system that stores all data on the disk/database so that you can detect a change in module version and instantly reboot the server. But the solution above should work for you for the time being :)
I've setup my application with Zend_Application. I have an _initAutoload() method in my Bootstrap.php wich looks like this:
public function _initAutoload(){
$this->bootstrap("frontController");
$front = $this->frontController;
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('Client_');
$autoloader->registerNamespace('Frontend_');
$autoloader->registerNamespace('Global_');
$autoloader->registerNamespace('Global_Helper_');
$autoloader->setFallbackAutoloader(true);
$modules = $front->getControllerDirectory();
$default = $front->getDefaultModule();
foreach (array_keys($modules) as $module) {
if ($module === $default) {
continue;
}
$autoloader->pushAutoloader(new Zend_Application_Module_Autoloader(array(
"namespace" => ucwords($module),
"basePath" => $front->getModuleDirectory($module),
)));
}
return $autoloader;
}
I have setup FrontController to prefix the default module also (seems more logical to me) $front->setParam("prefixDefaultModule", true)
I think I have the usual directory structure.
The problem:
I've set up subdomains for every module that I have. Everything works fine in the main main domain (www). The main module is frontend. If frontend is the default module then stuff works :). Ok. Now. For every subdomain, I have the same index.php but theres changed the env value. For client subdomain the env value is client etc. Each env value corresponds to my application.xml section. Each application.xml subdomain section (client, api, etc) extend the main section which is called defaults (currently theres a testing section also which enables errors etc, so every subdomain extends testing and testing extends defaults).
Each subdomain section of the application.xml changes the default module name. So for section defaults its frontend, for section client its client, etc.
Now
When I access domain.com/client or domain.com/api - its fine. Both API & Client use Client_Model_NameOfTheModel and like it supposed to - it's located application/modules/client/models/NameOfTheModel.php and the DbTable/NameOfTheModel.php
WORKS
BUT
When I access the the module from its respective subdomain (client.domain.com, api.domain.com, etc) and the default module has been changed from frontend to its respective subdomain module name - it ends working. It even doesn't output that "stack trace".
Warning: include(Client/Model/ContactLists.php) [function.include]: failed to open stream: No such file or directory in [heres-my-path-to-root]/library/Zend/Loader.php on line 136
Warning: include() [function.include]: Failed opening 'Client/Model/ContactLists.php' for inclusion (include_path='[heres-my-path-to-root]/library:.:/usr/lib/php:/usr/local/lib/php') in [heres-my-path-to-root]/library/Zend/Loader.php on line 136
Fatal error: Class 'Client_Model_ContactLists' not found in [heres-my-path-to-root]/application/modules/client/controllers/ContactListsController.php on line 4
I've tried 2 days to get it working. It just doesn't. It just works under the default domain and doesn't when the application.xml changes its default module to its subdomain name. Like that. This point is very very crucial currently because I can't continue and this app needs to be out of sandbox (in early beta) by the end of this week.
Thanks for anyone for some advice.
PS. Sorry for the poor English. It isn't my native tongue
This is just a cursory guess, but it looks like it might be working on default because of these lines:
$autoloader->setFallbackAutoloader(true);
$modules = $front->getControllerDirectory();
$default = $front->getDefaultModule();
foreach (array_keys($modules) as $module) {
if ($module === $default) {
continue;
}
Essentially, if your module is the default module it skips it, which means it falls back to the fallback autoloader i would assume, and if the default autoloader cant find your models, well theres the issue. Is the concat of the root path in the error and the path of the class its trying to load correct?
Also, this looks like it might be wrong
"namespace" => ucwords($module),
I would think it would need to be
"namespace" => ucwords($module) . "_",
Like your other namespaces.
Which version of ZF are you using?
Are you using a later 1.8 - 1.10 version?
If so, you should be using the resource in Zend_Application for a module. It sets up autoloading for forms, models, helpers, etc under your modules.
If you are using an application.ini you should have a line like this for each module :
resources.modules.module_name = "enabled"
http://framework.zend.com/manual/en/zend.application.available-resources.html#zend.application.available-resources.modules