Struggling to figure out how to achieve this. I want a login/registerform on same page when using tank_auth in CodeIgniter.
I was thinking of having something like this in my members controller (which extends My_Controller which extends Auth).
class Members extends My_Controller (
public function login()
{
//Already loggedin, do nothing
if ($this->tank_auth->is_logged_in()) {
return;
}
$this->view_data['login_by_username'] = true;
$this->view_data['login_by_email'] = false;
$this->view_data['show_captcha'] = false;
$this->view_data['login_form'] = $this->load->view('auth/login_form', $this->view_data, TRUE);
$this->view_data['register_form'] = $this->load->view('auth/register_form', $this->view_data, TRUE);
$this->v('members/login_register'); //In this view $login_form and $register_form is rendered
}
}
When I go to that page (localhost/members/login) the members/login-view is shown as expected, but in the action of the form for loginform it says:
http://localhost/members/login
Therefore when I click "Login-button" then it just calls members/login and that's not what I want. I want the action to be auth/login, but I still want to use my own template.
I hope you guys understand what I mean.... Please tell me if I'm doing anything wrong/thinking of this incorrectly.
UPATE:
In the actual template (members/login_register) it looks like this:
<div class="column">
<?php echo $login_form;?>
</div>
<div class="column>
<?php echo $register_form;?>
</div>
Maybe what I want to achieve is not possible?
Master View
You should use a Master view as a wrapper for your content.
That way you can easily pass in partial views(although not required)
However it will keep things neat. It also allows for easier control over
Admin/User Dashboards and your frontend.
Main Controller
class MY_Controller extends CI_Controller
{
public $template;
public function __construct()
{
$this->template = "master/template"; //views/master/template
}
}
To change it for an admin template(as an example of flexibility) you simply need to change it
the variable in the __constructor
class Admin_Controller extends MY_Controller
{
public function __construct()
{
$this->template = "master/admin/template"; //views/master/admin/template
}
}
Partial View(s)
Partial views can be used directly in other views.
You don't need a controller to call them.
These views are just stored in the buffer when they are
loaded by the loader class $this->load->view('', '', true)
A common approach is to create a folder inside /views called "partials".
This is where you would keep all you re-usable views(such as forms/widgets).
The Views(/views/partials/login_form)
<div>
<?php echo form_open('members/login', array('id'=>'login-form'))
</div>
The Views(/views/partials/signup_form)
<div>
<?php echo form_open('members/signup', array('id'=>'signup-form'))
</div>
The Views(members/index)
You can then combine the views
<div class="signup-form">
<?php echo $this->load->view('partials/signup_form')
</div>
<div class="login-form">
<?php echo $this->load->view('partials/login_form')
</div>
Login / Signup
In your members class you can create one method to show
the register/login form and then create methods to handle each of them individually.
class Members extends MY_Controller
{
public function index()
{
return $this->load->view('members/login_register');
}
public function login()
{
if(!$this->form_validation->run()){
return $this->index();
}
// form validation passed Ask Tank Auth
// to log the user in
$tank->auth->login();
}
public function signup()
{
if(!$this->form_validation->run()){
return $this->index();
}
// form validation passed Ask Tank Auth
// to register
$tank->auth->register();
}
}
Related
I have been trying to list a dropdown in the index page with data from database. I created a model and made some changes in controller to display it in my view page but making any change in the controller gives a blank page with 500 Internal server error in the console. Please help me out to sort this problem.
Table name: walker_type
Routes:
Route::get('/', 'WebController#index');
Model: ProviderType.php :
<?php
class ProviderType extends Eloquent {
protected $table = 'walker_type';
}
Controller: WebController.php
public function index() {
$walkerTypeList = ProviderType::all();
return view('website.index')->with(['walkerTypeList' => $walkerTypeList]);
}
View:index.php
#foreach ($walkerTypeList as $car)
<option data-icon="glyphicon-road" value="{{ $car->name }}"> {{ $car->name }} </option>
#endforeach
Had u declare your model below your namespace?
eg. use App\WalkerType;
also you forgot to declare a namespace to your Model.
it should have namespace App;
or if you have a folder for your model to make it more conventional.
you should have a namespace on each of your model.
eg. App\Model
and then use that in every controllers by declarin in between your namespace and class
eg.
namespace App\Controllers;
use App\Model\WalkerType;
class SomeController extends Controller{
protected $data; //this is a class variable that can call anywhere to your class by calling it this way $this->data
public function some_method(){
$this->data['variable_a'] = "some_value"; //this can call in you view later by $variable_a
$this->data['sum'] = 1+4; //result can be display in your view by calling the variable $sum
return view('someview',$this->data);
}
}
I hope this can help you for your project efficiently, cause we had experienced that we forgot to include some of the data that has been processed on the controller and needed to display in your view file but forgot to include.
Signup controller:
<?php
class Signup extends BaseController
{
public function getNew()
{
return Redirect::action('Signup#success');
}
public function success()
{
echo "successful";
}
}
?>
Routes.php
Route::controller('signup', 'Signup');
So when I go to localhost/signup/new it should throw out successful but ends with Unknown action [Signup#success]. error.
I read many same topics before but didn't help me at this case.
you need to use a certain format.. see RESTful Controllers
it says "Next, just add methods to your controller, prefixed with the HTTP verb they respond to"
change your Signup controller to:
<?php
class Signup extends BaseController
{
public function getNew()
{
return Redirect::action('Signup#getSuccess');
}
public function getSuccess()
{
echo "successful";
}
}
?>
and everything should work now..
this is my controller
<?php
class Site extends CI_Controller{
public function index(){
$this->load->view('home');
}
}
?>
This is my model
<?php
class AdminModel extends CI_Model{
//function to get all the questions
function getAllQA(){
$result=$this->db->get('question');
if($result->num_rows()>0){ //this checks if the result has something if blank means query didn't get anything from the database
return $result;
}
else{
return false;
}
}
}
?>
and this is my view PAge
<form method="get" action="">
<div id="container">
<?php
$this->load->model('adminmodel');
if($result=$this->adminmodel->getAllQA()){
foreach($result->result() as $rows){
echo $rows->question;
echo $rows->option1;
}
}
else{
echo "No question found";
}
?>
</div>
</form>
So am calling the model in view called home.php page but its showing an error Call to a member function getAllQA() on a non-object in So but when am calling the model in controller its working fine but why is it showing error when am loading and calling the method in view page
Load your models inside the constructor of your controller
your controller should be like this
<?php
class Site extends CI_Controller{
function __construct()
{
parent::__construct();
$this->load->model('adminmodel');
}
//if you want to pass data or records from database to view do as blow code
public function index()
{
//define an empty array
$data = array();
$records = $this->adminmodel-> getAllQA();
$data['records'] = $records;
//pass the data to view you can access your query data inside your view as $records
$this->load->view('home',$data);
}
}
?>
You should not be loading Models from view files. Codeigniter is a MVC framework which means that all communication with the model should be handled by the controller.
The technical reason that this isn't working is likely that the view file is not a php class and therefore $this does not exist. Thats regardless, if you want to do something like this, don't use codeigniter!
not sure if you've done it.
Usually you call your model from within the controller.
In your case it would be:
class Site extends CI_Controller{
public function index(){
// load your model
$this->load->model('adminmodel');
// do your fancy stuff
$data['allQA'] = $this->adminmodel->getAllQA();
// here you can pass additional data e.g.
$data['userdata'] = $this->adminmodel>getuserinfo();
// pass data from controller --> view
$this->load->view('home',$data);
}
}
You can access the data in your view file by acessing $allQA or $userdata respectively. E.g.
foreach ($allQA as $qa){
echo $qa->question . "<br>" . $qa->option . "<br>";
}
or somewhere within a div
<div class="userbadge">
<?php echo $userdata; ?>
</div>
I am using HMVC with CodeIgniter.
I have this in my testmodule controller:
public function index()
{
$this->view_data['main_content'] = 'frontpage';
$this->load->view('template', $this->view_data);
}
And this in my view template.php of that controller that is loaded by this controller:
<?php
$this->load->view('includes/header');
$this->load->view($main_content);
$this->load->view('includes/footer');
?>
but, when I var_dump($main_content) in the view and die() it shows null instead of frontpage
How, come? I don't get it at all.
If you want to use $this->view_data you have to declare $view_data as a property first (at the top of your controller):
class TestModule extends CI_Controller
{
public $view_data = array();
public function index()
{
// Now you can use $this->view_data in this function:
$this->view_data['main_content'] = 'frontpage';
$this->load->view('template', $this->view_data);
}
}
I am newbie in Zend framework.And i have made sample project in netbeans.And it is working properly displaying index.phtml .But , I need to call my controller.What I have tried is below.
IndexController.php
<?php
class IndexController extends Zend_Controller_Action
{
public function init()
{
}
public function indexAction()
{
firstExample::indexAction();
}
}
And I have deleted all the content of index.phtml(just a blank file) , coz i don't want to render this view.
My custom controller is:
firstExampleController.php
<?php
class firstExample extends Zend_Controller_Action{
public function indexAction(){
self::sum();
}
public function sum(){
$this->view->x=2;
$this->view->y=4;
$this->view->sum=x + y;
}
}
?>
firstExample.phtml
<?php
echo 'hi';
echo $this->view->sum();
?>
How to display sum method in firstExample.php.
It just shows blank page after hitting below URL.
http://localhost/zendWithNetbeans/public/
I think after hitting on above URL , execution first goes to index.php in public folder.And I didn't change the content of index.php
You are using controller (MVC) incorrectly, Controller should not do any business logic, in your case sum method. Controller only responsible controlling request and glueing model and view together. That's why you have problems now calling it.
Create Model add method sum, and use in any controller you want. From controller you may pass model to view.
Here is example: http://framework.zend.com/manual/en/learning.quickstart.create-model.html it uses database, but it's not necessary to use with database.
Basically your sum example could look like:
class Application_Sum_Model {
public function sum($x, $y) {
return ($x + $y);
}
}
class IndexContoler extends Zend_Controller_Action {
public function someAction() {
$model = new Application_Sum_Model();
//passing data to view that's okay
$this->view->y = $y;
$this->view->x = $x;
$this->view->sum = $model->sum($x, $y); //business logic on mode
}
}
Please read how controller is working, http://framework.zend.com/manual/en/zend.controller.quickstart.html