How can I have multiple actions in my layout from different controllers/modules?
I tried this:
<div id="login"><?php $x=new User_LoginController; $x->LoginAction() ?>
<div id="news"><?php $x=new Site_NewsController; $x->ShowAction() ?>
You'll want to implement view helpers, specifically the placeholder() view helper.
For example to render a login form in any or all pages of your application, we start with a placholder for the form in our layout or view script:
<!--layout.phtml-->
<div>
<?php echo $this->layout()->login . "\n"?>
</div>
I use an action helper to prepare the form for display:
<?php
/**
* Prepares login form for display
*/
class My_Controller_Action_Helper_Login extends Zend_Controller_Action_Helper_Abstract
{
/**
* #return \Application_Form_Login
*/
public function direct()
{
$form = new Application_Form_Login();
//this is the url of the action this form will default too
$form->setAction('/index/login');
return $form;
}
}
now from any controller or front controller plugin the placeholder can be setup:
public function preDispatch()
{
$this->_helper->layout()->login = $this->_helper->login();
}
now the login form will display in any action from this controller that uses layout.phtml as it's layout. I'll let you discover plugins yourself.
Using helpers with placeholders would usually be the prefered way to accomplish what you want. However if you absolutely must display an action inside of anothers view you can use the Action view helper, just be aware that performance may suffer.
<div id="login">
<?php echo $this->action('login', 'login', 'user'); ?>
</div>
I didn't get what exactly you want?
What I guessed is may be you want to call this function(action) from layout to show what is
returning from there into layout......
Related
I have a form in a tpl file:
<form action="{$link->getModuleLink('virtual_pos', 'validation', [], true)|escape:'html'}" method="post">
...
</form>
On submit I would like to get all the variables from the form and pass them to the controller 'validation'.
I don't wanna use any JS. It is a payment module for a store.
How can I do this?
I have found a solution in another thread.
When the link to the controller is created you can fill the variables that you need in the empty array parameter:
<form action="{$link->getModuleLink('virtual_pos', 'validation', ['id'=>$cart_id], true)|escape:'html'}" method="post">
Then in the controller you can get the data with the super global
$id_from_form_submit = $GET['id'];
If you know any other option please let me know.
In your module create a file controllers/front/validation.php.
There you need a class:
class virtual_posValidationModuleFrontController extends ModuleFrontController
{
public function postProcess()
{
/* where you get the values and validate the order */
}
public function initContent()
{
parent::initContent();
/* where you set data for a last page order confirmation */
}
}
Have you created this already?
I have two forms on a page both submits the data via POST method. I want to handle both requests on the same url i.e /home and use different controllers and methods for both forms. Below are the two routes.
Route::post('home' ,'FacebookControllers\PostsController#save');
Route::post('home' , 'FacebookControllers\MessageController#storeMessage');
In PostsController#save I am checking
if(isset($_POST['submitPost']) && $_SERVER['REQUEST_METHOD']=='POST'){
//do something
}
and in MessageController#storeMessage I am doing the same for other form
if(isset($_POST['sendMessage']) && $_SERVER['REQUEST_METHOD']=='POST'){
return "got it";
}
The problem is that only second route works. I don't if I am doing right or wrong. Please lead me to the right direction.
Route::post('home' ,'FacebookControllers\PostsController#save');
Route::post('home' , 'FacebookControllers\MessageController#storeMessage');
won't work - how should laravel determine where to post to? The way I would go is to create two jobs and one route, then check for the value in Request and dispatch the correct job.
First, create two job classes with
php artisan make:job FacebookSave
php artisan make:job FacebookStoreMessage
The generated file will look much like this:
<?php
namespace App\Jobs;
use App\Jobs\Job;
use Illuminate\Contracts\Bus\SelfHandling;
class FacebookSaveMessage extends Job implements SelfHandling
{
/**
* Create a new job instance.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
//
}
}
In the job's handle() method you can do what you wanted to do in your Controller.
Now the route, let's do
Route::post('home' ,'FacebookControllers\PostsController#findAction');
and according to this in your PostsController, add a method (I called it findAction) like this:
public function findAction(\Illuminate\Http\Request $request) {
if ($request->has('submitPost')) {
return $this->dispatch(new \App\Jobs\FacebookSave($request));
} else if ($request->has('storeMessage')) {
return $this->dispatch(new \App\Jobs\FacebookStoreMessage($request));
}
return 'no action found';
}
This way the correct action will be performed depending on the submitted value.
Change the job's constructor to something like:
public function __construct($data)
{
$this->data = $data;
}
And you can access the submitted values from the forms you have submitted inside the job's handle() method with $this->data
If the purpose is only to maintain the URL that appears in the browser, you may apply a trick in which you submit your post request to different ROUTES that are specifically exist for processing purpose and then redirect back to the same URL.
For example:
Route::post('home' ,'FacebookControllers\PostsController#save');
Route::post('message' , 'FacebookControllers\MessageController#storeMessage');
Then in your view , you maybe have two diffrent forms in the same view:
<!-- the first form -->
<form action="{{url('home')}}" method="post">
Enter your post:
<input type="text" name="post" />
<button type="submit" >Save</button>
</form>
<!-- the second form -->
<form action="{{url('message')}}" method="post">
Enter your post:
<input type="text" name="post" />
<button type="submit" >Save</button>
</form>
Now in your controller, in the both actions do something like :
public function save(Request $request)
{
// do stuff
$obj->save()
return redirect('home')->with('status' , 'you have saved your post')
}
public function storeMessage(Request $request)
{
// do stuff
$obj->save()
return redirect('home')->with('status' , 'your message has been saved')
}
By doing so, the URL will remain same for the user, after the process is done the user will be redirected back to the same URL with the status.
May be this is not a good coding practice, but it solve the problem by maintaining the same URL while having multiple posts to the same controller in the same view.
The most important point here is that you have to strictly prevent an error during the process by using conditions and redirect to the same URL in case of any problem, otherwise the action will try to show the user the tricky ROUTE (yoursite.com/message) for any error.
I have a template design for each page and it is working without any issues, now i have a problem with form validation and template.
my_controller //inside my controller
function __construct()
{
parent::__construct();
$this->load->library('template');
}
public function page1(){
$this->template->write('title', 'COMPANY');
$this->template->write_view('content', 'company');
$this->template->render();
}
public function validation(){
//for page1 form validation
//form validation here
if ($this->form_validation->run() == FALSE){
// here i need to call my page1 again to update the form error
// option1: $this->load->view('page1'); // this is just loading page1, not loading with my template, template file has header, footer, js and css file includes
// option2: $this->template->load('template', 'page1'); // it is loading page withing page
// option3: $this->template->write_view('content', 'page1');
// $this->template->render(); // this is same as option2
} else {
$this->load->view('formsuccess');
}
}
So my question is how do i can call same view (within one controller) for multiple time when it is already loaded with template?
EDITED for more clarity
My template file is just single file ( not separated like header, footer) will have all header information and footer information with common style sheet and script files.
I am having
<?php $content ?>
variable name inside the template to pass my content from controller.
for example see my page1 function(),
$this->template->write('title', 'COMPANY'); // to write a value on variable
$this->template->write_view('content', 'page1'); //view page to write on template
$this->template->render(); // final template render
This page is having form validation, if my form validation is FALSE, how do i can reload the template page with validation error?
I have tried so many way, please check my option1, 2, 3, i never got a solution to solve this, now please tell me how do i can solve this?
If I understand correctly, you want to display the validation errors?
Inside your View file, put the following:
Short IF-Statement Form:
<?php if ( validation_errors() ) :?>
<div>
<?php echo $validation_errors();?>
</div>
<?php endif;?>
Regular If-Statement Form:
<?php if ( validation_errors() ) {?>
<div>
<?php echo $validation_errors();?>
</div>
<?php }?>
Validation Errors Doc
I have created a fully custom view, I want this view to only show certain fields in an editview format so I can update records. But this view is to be different from the normal editview. How do I add a custom metadata file to this view that will allow me to define the form and fields that I need? The view is tied to a custom button and currently just shows "works". This is working so far just need to understand how to define the layout.
the controller:
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
class CustomCasesController extends SugarController {
function action_resolve_Case() {
$this->view = 'resolve_case';
}
}
The view :
if (!defined('sugarEntry') || !sugarEntry)
die('Not A Valid Entry Point');
require_once('include/MVC/View/SugarView.php');
class CasesViewresolve_case extends SugarView {
public function CasesViewresolve_case() {
parent::SugarView();
}
function preDisplay() {
parent::preDisplay();
}
public function display() {
// include ('test.php');
echo "works";
}
}
Old, but still may help someone...
You can :
work inside the display function. Everything you do or echo here will be shown inside the main Sugar app screen (navbar, footer, etc) so it will look native.
Work directly inside the controller and echo everything out.
Build your form with the fields you want to edit and have as action a function in controller where you can use the bean->save() method for the POST results.
<form name="whatever" method="POST" action="index.php?module=Contacts&action=yourcustomaction">
ex:
`$contact = new Contact();
$contact->retrieve("{$_REQUEST['contactId']}");
//be sure to send the id in the button/link to the view
$contact->first_name =$_POST['first_name'];
$contact->last_name =$_POST['last_name'];
.....
$contact->save();`
Not very elegant but the only other option I know of is working with smarty templates which I'm not familiar with.
If anybody has a better solution, please post it.
I have done very much research on this topic but I cannot find how i can do this. I am trying to add data to the $data parameter in a view that is being called from the controller of another view. However, any data i add to the subview via the subcontroller cannot be accessed by the subview. However, when I try to pass data to the subview via the client view, it works just fine. Most of the fixes on SO seem to reference just calling the $key in $data['key'] rather than $data so that doesn't seem really relevant here...
I have two classes:
welcome.php - a page
welcomemenu.php - a set of controls intended to
be loaded into welcome.php
Here is my Client Controller (the page that it's on, welcome.php), which stores the return val from subview $welcomemenu in its own $data array...:
<?php
class Welcome extends CI_Controller {
function __construct() {
parent::__construct();
}
function index() {
//echo 'this is the Welcome index function';
$data['clienttestdata'] = 'data from welcome.php!';
$data['welcomemenu'] = $this->load->view('welcome/welcomemenu', $data, true);
$this->load->helper('url');
$this->load->view('templates/header');
$this->load->view('pages/welcome', $data);
$this->load->view('templates/footer');
}
}
And here is the client view ("welcome_view.php" - seems simple enough. The $welcomemenu var is where I put the returns from my component class...):
<section id="allWelcomeContent" class="mainBody">
<header id="mainPageHdr" class=mainPageHdr>
<!-- other stuff from my header -->
</header>
<!-- this is where i want to put the welcome menu... -->
<section id="mainWelcomeContent" class="mainContent">
<div>
<?php echo $welcomemenu;?>
</div>
</section>
</section>
And here is the Controller for my sub-component welcomemenu.php:
<?php
class Welcomemenu extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$data['menu_items'] = array('About', 'Portfolio', 'Resume', 'Fun', 'Blog');
$data['testdata'] = 'data from welcomemenu.php!';
$this->load->view('welcome/welcomemenu', $data);
}
}
And lastly: Here is the sub-view that is supposed to get data from its own controller, but cannot, even though it can take data from a calling client (i.e., the $clienttestdata shows up fine but $testdata doesn't)!
<section>
<!-- TODO: make this element repeatable so content can load from controller and/or model. -->
<div id="divMenuItems">
<?php echo $clienttestdata;?>
<?php echo $testdata;?>
</div>
</section>
Still i couldn't find any proper solution. if anyone then please give me
When you're including the welcomemenu partial in your Welcome/index method, you have to remember that the view does not go through its own controller. Instead, its contents are returned as a string and stored as a parameter. It gets all of its own parameters through the ones you send to it via $data:
$data['welcomemenu'] = $this->load->view('welcome/welcomemenu', $data, true);
This view will thus have access to everything in $data so far - nothing extra is added through the Welcomemenu controller. So, in the above case, it will have:
array
(
'clienttestdata' => 'data from welcome.php!'
)
If you add the parameters you need to $data (as $data['testdata']), your sub-view will have what it needs.