Submit a Form to Custom Route in osclass - php

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.

Related

How should I structure my routes and controllers in Laravel?

I've got two buttons on my page, which each have different behaviours when clicked. Currently I've got my routes and controllers set up like this...
routes/web.php
Route::post('/users/button1clicked', 'UsersController#button1clicked');
Route::post('/users/button2clicked', 'UsersController#button2clicked');
app/http/controllers/UsersController.php
class UsersController extends Controller
{
public function button1clicked(Request $request){
//Do something
}
public function button2clicked(Request $request){
//Do something else
}
}
It works ... but I don't think I'm following the correct convention for my controller, because controllers should just have the standard actions (index, create, store, show, edit, update, destroy).
What would be a better way for me to structure this code?
What does each button do? For example, if the button submits a form to 'save' an entity (say a 'Post' in a Blog app) then the form action can direct to the PostController#store. If the button click is intended to show a html form to create an Post, then the it can lead to PostController#show. The table below from the Laravel docs will help you.
Route definitions conventions
Please also see https://laravel.com/docs/7.x/controllers#restful-naming-resource-route-parameters.
If you are using ajax or axios to make async calls then you can call a function using button events (on-click for example) and that function can make a post (or any other async call to the relevant controller method ( PostController#store). Please let me know if you'd like an example.

How are functions inside controllers accesed in SocialEngine?

For example I have edit profile page which have a form for editing the summary. the file's name is Index.tpl.
In form I have a text field, I have added the saveSummary() in the controller i.e. controller.php. How can I invoke the given function on clicking on submit button of form.
Answering directly to your question, you should submit the form to /save-summary/ action if it's called saveSummary() in your controller. Of course don't forget to include prefix of the route.
Generally your approach is not correct because you're trying to use different actions for displaying content and processing the form – you can easily do both operations in one action. Check for isPost() and getPost() in other controllers – this methods are used to divide parts of action responsible for simply getting and displaying content and for processing form data.
You can access controller functions by using action handle at the end of function name ex -
class Mymodule_MytestController extends Core_Controller_Action_Standard{
public function saveSummaryAction(){
.......
}
}

Using several modules in the same view

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.

CakePHP - REST POST functionality not working with default mapping

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.

CodeIgniter: some doubts about HMVC and Views

I've just discovered HMVC Modular Extension for CodeIgniter https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/wiki/Home and it seems perfect for my needs but I have some question.
Let's say I have two controllers:
Site which is the main controller and is used to show site's pages and may call methods of Users controller for example to show a form
User controller is used to authenticate users, to show login/sign up forms...
Now I have these questions:
If the user access the User controller directly (mysite.com/user/method) I want to show a full page while if i load a method of User from within the Site controller I want to show only a form (for example), is this possible?
What happens to view of a module loaded from another module: is the view shown automatically or i need to show it manually and how does the view behave?
If you method is being called via Modules::run()
There is a third optional parameter lets you change the behavior of
the function so that it returns data as a string rather than sending
it to your browser.
Eg:
//put underscore in front to prevent uri access to this method.
public function _module1()
{
$this->load->view('partial_view', array('some data'=>'some data'), TRUE)
}
call it inside your SITE view easily
Modules::run('User/_module1')
// should show whatever is in partial_view ie: a form
//an alternative is to pass in any params if the method requires them
Modules::run('User/_module1', $param)

Categories