I am new to CakePHP and am attempting to configure my web app to use REST.
I am using CakePHP version 2.0.4.
Following the CakePHP Cookbook, I have entered the following into the routes.php file ('Apples' pointing to the ApplesController.php controller):
Router::mapResources('Apples');
Router::parseExtensions();
and in the controller itself (ApplesController.php), I have added the following component:
public $components = array('RequestHandler');
Also in the controller, I have an add action which echo's a string (as follows):
function add() {
echo 'The add action has been called';
}
Finally, I created a view (index.ctp located in app/View/apples/) which uses a HTML form with the method POST and the action "/apples/" to submit.
<h2>Home<h2>
<form action="/apples/" method="post">
<input type="submit" value="Submit" />
</form>
Now according to the the CakePHP Bakery (http://bakery.cakephp.org/articles/rightwayindia/2010/01/11/restful-web-application-development-in-cakephp), the mapResources should automatically map POST to the 'add' action of the controller specified in the routes.php file, therefore when the button is clicked, should echo the string in the add action.
This only works when you change the action parameter in the HTML (in index.ctp) to:
<form action="/apples/add" method="post">
and explicitly point to the add action.
I may be wrong, but I thought that by configuring REST in the routes.php file should automatically map the specific REST methods to the actions such as add, edit, delete etc.. (stated on the web page linked above) to the controller stated in the paramater of the mapResources function.
I have also tried custom REST routing but this is also not working. However, It would be nice for it to work with default mapping rather than customising it.
Any suggestions would be appreciated. Thanks.
Related
I am making an osclass plugin. I have added a page for my plugin in the admin section of the site. I have also created a route for my plugin.
What I would like to do is create a form on my plugin's page in the admin. The action attribute of this form will be the base url of my custom route. The form will then add a query string of parameters to the base url when the administrator submits it. Specifically, the query string should include an id parameter, for example ?id=1.
Here is what I have:
<form action="<?php echo osc_route_admin_url('my-custom-route'); ?>" method="get">
<h2>User Profile form</h2>
<select name="id">
//...options, each one's value is a registered user
</select>
<input type="submit" value="Submit">
</form>
I have tested this form. Instead of dropping me off at my custom route url, osclass redirects me to the url of the admin dashboard (with the correct id included in the query string along with an octoken). I need it to trigger my custom route.
I have tested the custom route by printing a link to it on the same page where the form is. When accessed by clicking the link, the custom route works fine.
Thanks to anyone who can help!
I did eventually figure this out, or at least one way to do it. You can use the renderplugin_controller hook. This hook is fired anytime a user requests the url to a custom route in the admin area. To add a callback called controller to it, I added a line in the constructor of my plugin's bootstrap class:
function __construct() {
//...
osc_add_hook( 'renderplugin_controller', array($this, 'controller'));`
//...
}
Then, the hooked controller method checks for a route parameter in the url when the renderplugin_controller hook is run. If the route parameter exists, and it belongs to my plugin, I have a separate controller class that gets instantiated and processes the request from this point forward. I can test if the route belongs to my plugin by matching the route name to a prefix specific to my plugin.
public function controller(){
if (is_null(Params::getParam('route'))) return;
//strict type checking
if ( false !== strpos( Params::getParam('route'), 'my-prefix' )){
$controller = new Admin_Controller();
$controller->doModel();
}
}
As far as the Admin_Controller class goes, it should extend the AdminSecBaseModel class, according to the examples I've seen. The basic structure for it is:
class Admin_Controller extends AdminSecBaseModel {
//property to hold my model class
public mUser;
public function __construct(){
parent::__construct();
//instantiate my model class
$this->mUser = User::newInstance();
}
public function doModel(){
/*
I only have one route in my plugin.
If you had multiple you could switch over them
with switch(Params::getParam('route'))
*/
$id = Params::getParam('id');
$user = $this->mUser->findByPrimaryKey( $id );
//do whatever you want with the user object
}
}
If you want another more complete example of how to use the renderplugin_controller hook to handle a form submission to a custom route, I recommend checking out this repository (it's for the osclass user custom fields plugin). I basically copied what was done there. In particular, the UserCfAdmin class and the UserCfController_Admin class showed me exactly how to do this.
You can find another guide to making a controller for your custom routes here.
Note: I am using osclass v3.8 on the site i was making this for.
I have the following from in yii.
<form action="" method="POST">
I would like to to the following:
<form action="/protected/plans.php" method="POST">
However, that doesn't work. What is the proper way for providing the link in the form action in yii. Thank you for the help.
You can't access from url to files in /protected folder. It's private forlder to use only on server side. Move you file plans.php to project's root directory or create another folder near /protected.
As SiZE said, You can't access the files inside protected, because yii projects have a .htaccess file in the root and this file has deny form all line. First way is to move file out of protected folder. But I think it's not a good choice at all. You are using an MVC Framework and you should use this structure in your project. For example you should use MVC url pattern, not default pattern. If you want to use plain url pattern, Why do you use mvc framework?!
I recommend you to learn more about mvc architecture and yii framework.
In order to use Yii effectively, all requests from your web page should go through the controller mechanism build into Yii.
You can read more about this here.
http://www.yiiframework.com/doc/guide/1.1/en/basics.controller
You will have a create a controller, which consists of one or more actions. Your web form will then send a request to invoke this action.
As an example, so you have a controller Plans that supports an action create
// In file protected/controllers/PlanController.php
class SiteController extends CController
{
public function actionCreate()
{
// Put your code here to create a new plan
echo "I am going to create your plan";
some_other_code();
Yii:app()->end();
}
public function actionShowform()
{
$this->render("form");
}
}
You can then create a url to access the action.
// In file protected/views/plan/form.php
<form action="<?php echo Yii::app()->createUrl("plan/create"); ?>" method="POST">
<!-- form details here -->
</form>
It's my first time I am using zend studio. I am working on existing php application and successfully running on server. I am trying to add the new view in the application under specific folder. For this first I am appending the controller to add a new action and created the new view with same name. Unfortunately the controller action is unable to display the view.
Here is my controller code.
public function testAction() {
mysqli_close($this->db);
}
I am not passing any variables in my controller action because my view is php free code. I don't need any variable from action on my view. I am redirecting to view using this code.:
url(array('controller'=>'dashboard', 'action' => 'test', 'brand'=>$this->escape('nrgrs')),'default',true) ?>">
Unfortunately my redirection fails when I click on the link. Any idea?
Thanks,
Ashnav
In a case where the form action is set to something like:
action="<?php echo JRoute::_('index.php?option=com_test&layout=edit&id='.(int) $this->item->id); ?>"
and the form contains and hidden input:
<input type="hidden" name="task" value="testctrl.save" />
How does joomla route to the controller method?
I would understand if it had the task in the form action, but I can't see how it picks up the task from the hidden input in order to route to the appropriate method in the testctrl controller method
It's not that complicated. In your com_mycom directory there is a file called mycom.php. In it you have some lines that look like this:
$controller = JControllerLegacy::getInstance('Contact');
$controller->execute(JFactory::getApplication()->input->get('task'));
$controller->redirect();
See an example here:
https://github.com/joomla/joomla-cms/blob/staging/components/com_contact/contact.php#L15
So that's what takes the task and instantiates an instance of that controller object, and pulls the task from the hidden form input value that you pointed out. It passes the task to the controller from there.
The controller receives the request here:
https://github.com/joomla/joomla-cms/blob/staging/components/com_contact/controller.php#L19
You might be asking "why don't I see it receiving the task that the component file sends it?". Well that's because the controller for this component is a child-class of the JControllerLegacy class:
https://github.com/joomla/joomla-cms/blob/staging/libraries/legacy/controller/legacy.php#L701
public function execute($task)
{ ... }
This function is the execute function which receives the task from the component. This is the parent class of your controller task. Hopefully this all makes sense!
When you set the controller name explicitly with hidden fields
<input type="hidden" name="task" value="testctrl.save" />
or
<input type="hidden" name="controller" value="testctrl" />
<input type="hidden" name="task" value="save" />
or even not specifying the controller with task , just used it with view name.
All the cases your component file like com_test have a file with test.php
it includes Joomla library files.
jimport('joomla.application.component.controller');
when you check the library file it have two function for getting related controller and models.
createFileName() and getInstance() in libraries/joomla/application/component/controller.php
These two function do the task.
The above files are applicable only for Joomla 1.5 to Joomla 2.x
Edit
For Joomla3.x
In Joomla 3. x The file structure little bit changed.
Instead of jimport('joomla.application.component.controller'); Joomla 3.x uses
$controller = JControllerLegacy::getInstance('Content');
This will call the JControllerLegacy class in libraries\legacy\controller\legacy.php
you can find the same functions createFileName() ,getInstance() on the above path.
Hope its helps..
I'm developing a web application with Zend Framework 1.12, which is something new to me, and I'm not sure about the way to do something I want to.
EDIT: When I talk about Module, I mean Controller, sorry for that, I still mistake the terms ...
On my home page, the module Index, I made what I wanted to do with it, created several actions and all the stuff, but I'd like to add a search engine I'll make myself.
The problem is that I'd like to create the search engine as a separate module named Search, for example, but put the SearchForm in the home page. Hitting submit would send the datas from the form to the Search module.
I don't quite understand how to do that without having to go to /search to access my form and every associated actions.
Do I have to use a View Helper ?
Also, the searchForm in the front page would be some sort of QuicKSearch and accessing /search would show a more elaborated form for the research.
Can someone explain me how to access the searchForm from the Index module or redirect me to the part of the documentation talking about that ? My research are unsuccessful and Google doesn't help me either.
EDIT: When I talk about Module, I mean Controller, sorry for that, I still mistake the terms ...
First of all, build the searchform as viewHelper, then you can reuse it in several views.
The action attribute in form snippet set to searchModule/controller/action.
Additionaly make research about viewHelpers and Forms in Zend Documentation.
I actually prefer to do this as a an action helper and then just use a standard placeholder view helper to present the search form.
let me demonstrate:
the actual action helper just initiates a form and prepares it for display. I'll leave the form structure to you.
//the action helper
//Just fill in the args for the form to be displayed
class NameSpace_Controller_Action_Helper_Search extends Zend_Controller_Action_Helper_Abstract
{
public function direct($action, $label = null, $placeHolder = null)
{
$form = new Application_Form_Search();
//set the action
$form->setAction($action);
//set the submit button text
$form->search->setLabel($label);
//set the hint text displayed in the form window
$form->query->setAttribs(array('placeholder' => $placeHolder,
'size' => 27,
));
return $form;
}
}
I put the helper in the predispatch method of the controller so that each action in the controller can use the search form with having to build it in every page.
//to use the helper in your controller
class IndexController extends Zend_Controller_Action
{
public function preDispatch()
{
//setup action helper and assign it to a placeholder
$this->_helper->layout()->search = $this->_helper->search(
'/index/display', 'Search Collection!', 'Title');
}
//in your view script
<?php echo $this->layout()->search ?>
I like to put the placeholder in my master layout.phtml so that any time I populate the placeholder it will display. Now all you have to do is style it however you want.
Remember: As with any html form the action parameter is just a url so any valid url can be assigned to the form action. In this example I used the /controller/action parameters, but there are many other ways to pass a url to the form. The url helper comes to mind as good way to do it.
url($urlOptions, $name, $reset, $encode): Creates a URL string based
on a named route. $urlOptions should be an associative array of
key/value pairs used by the particular route.