CakePHP: Use function from Plugin Controller in Main Controller - php

This should be simple but I've spent over an hour trying to figure it out so thanks for your help.
I've got a CakePHP plugin, Usermgmt, with a controller located here:
./app/Plugin/Usermgmt/Controller/UsersController.php
I'm trying to call a function, userIdFromUsernameAndPassword(), in that controller from one of my main controllers using something like:
$userID = $this->UsersController->userIdFromUsernameAndPassword( 'user#host.com','pass' );
What do I need to import/include/initialize to be able to get this working?
I've tried various import statements such as App::uses('UsersController', 'Usermgmt.Controller'); at the top of my file, but haven't gotten anywhere.
Thanks!

Short answer: Use OOP and extend the other controller. Also get an understanding of MVC. You are not supposed to use a method of a controller inside another controller, in CakePHP this should be done as a component. They can be shared between controllers. Check the CakePHP Book.
Also the name of the plugin and the method name indicate that this is a bad plugin. This sounds like somebody did not know about the Auth component of CakePHP. Again, check the book for the AuthComponent. You want a custom authentication adapter.
If the user is logged in you can get its id by calling $this->Auth->user('id'). Read the chapter about Auth. If you want a properly done user plugin check out: CakeDC Users

Related

Use folders Inside CodeIgniter Model and Controllers

I'm using CodeIgniter Framework to build a php Website. I need to organize my controllers like:
application/controller/HRModuleController/<myControllers>
application/controller/AccountModuleController/<myControllers>
Can I use that way to organize my controllers and Models?
If I can how do I call the controller in the View?
<?php echo HRModuleController/EmployeeController/select_all ?>//is this Correct?
For Controllers you can do like this, By adding a root controller inside /application/core
Refer to this link -
http://glennpratama.wordpress.com/2009/10/20/multi-level-subfolder-for-controller-in-codeigniter/
No, you can't do it like that. I suggest you first read the manual. Reading an hour in it will save you dozens.
To answer your question:
When the user clicks a link, the request will go to your controller. In your controller you call your views (and models). The way to go to the next controller is by using a link, like:
Click here!
you can call the controller by $this->EmployeeContoller->select_all(arguments);

Adding extra field on opencart checkout

I'm using opencart v.1.5.1 How do I add extra text area on opencart checkout page?
I've added Step 6 using photoshop, how do I code that on opencart?
I don't know about your programming background, but you need to be a little bit more than familiar with PHP, AJAX, JavaScript and of course MySQL, you should also know about the MVC architecture. If you think you have enough knowledge of those then here is what you need to do:
Create a controller class and name it lets say "remarks"
class ControllerCheckoutRemarks extends Controller {
public function index() {
// Your code
}
}
add the code you need to process the data, you can get some ideas by looking at "checkout/guest/shipping" controller class. What you need is quite similar to what that class does.
if you need to interact with the database in order to process the data then you need to create a Model class in the same route under the Model folder and write your functions in it. again you can get the idea from other model classes. But I don't think you need this, probably you want to add this information to your order, in order to do that you should modify the Order class, both Controller and the Model !
Finally you need to create a template file for it, once again open the template file for "checkout/guest/shipping" and see how they did it, then you create your own.
For the controller and model classes make sure you name them properly.
There is one more thing you need to do, The controller class for the step 5 sets the redirect to the next step which by default is the Confirmation. Change this line and make it redirect to your section and do the same thing for the new section and redirect it to Confirm:
url: 'index.php?route=checkout/shipping'
I can only guide you what to do, how to do it is your job :D If you are a programmer then I don't think you'll have any problem with it. if you're not i guess you'll need someone to do it for you :)

handle invalid controller explicitly in Zend framework

I am a new Developer in Zend and I am working on a very simple application in Zend wich will allow me to publish the pages.
to do so, I need to create a way to get the controller name and action name if it does not exist.
e.g I have a controller named page and in the view action of that controller I display the page content based on the id I Pass. So he url will be:
http://localhost/page/view/slug/some-slug-value
I want that if I write the following link,
http://localhost/some-slug-value
instead of displaying Invalid controller error, it should also serch the db for that slug and open the view action in the page controller.
I don't want to change the URL http://localhost/some-slug-value even if i have to show some error.
If i had to process the invalid action, I can do it via the __call() function in that controller. so I need a __call function for handling invalid controllers.
As I am a new in Zend, please also mention where I have to write that code which you are going to suggest.
Thanks in advance
Zend_Controller_Plugin_ErrorHandler can handle the exception due to missing controller/action and is enabled by default. You can put your logic inside.
I have answered a similar question before using a custom plugin here.
Basically, it checks if a request can be dispatched and in the event it cannot, it redirects to another controller/action. This happens before the ErrorHandler plugin allowing for the ErrorHandler to handle errors only as intended.
EDIT
I should note that inn the plugin on the linked question, I referenced the username. In your case, you will have to substitute slug for username.

php- new way of posting form

im designing a way the form posts its data.
e.g if we have a login.php, if the user submit we normally post it back to login.php and process it. which means if we have other pages like register.php, editprofile.php, we have to redo the process again. so normally we would do something like this in each page:
if($_POST["btnsubmit"]) {
//do smth
}
Im thinking of doing a common postForm.php which accepts all post requests, pass the data to the respective library and process it.
is this a good idea??
It is definitely a good idea! What you're describing is called a controller, from the Model View Controller pattern. I recommend checking out Symfony, which is a great MVC web framework for PHP.
A single Symfony controller (with a name like actions.class.php) can handle all of the posts and gets, plus the routing to get you there. By Symfony convention, a call to http://mywebsite.mydomain.com/home will run the executeHome function in the main controller. A form on that page could, for instance, post to /attemptLogin, and (again, by convention) Symfony would run the executeAttemptLogin function in this same controller file.

How can I get current method, class, parent class's folder for all controller in CI before every public method get called?

I'm building a rbac in CI 2.1.4. I don't want to use opensource rbac system, because I'm building on an existent system. My resource is designed to: module_name::controler_name::action_name, I want to get the module_name::controler_name::action_name for the current executed action, eg. controllers/zoo/suggest.php's index action. so the module name is zoo, controller name is suggest, action name is index.
But when I'm in this index action, how I can get the zoo,suggest? Yes, it's simple to write some code to get them, but I have hundreds of actions and controllers. I found this way: http://stackoverflow.com/questions/11372277/how-can-i-create-a-method-that-gets-called-every-time-a-public-method-gets-calle, but I don't want to write nearly same code in every controller. I just want to write once.
I want to use post_controller_constructor, but this hook does not support the acquiring of my would-be-executed method and controller and module name.
How can I succeed my goal?

Categories