how can create pages inside a codeIgniter controller method - codeigniter

I am building a site using codeIgniter. My problem is I cant control my controller if my URI contains more then two segments. see http://example.com/products/woven/men. I want to make category of men look like http://example.com/products/woven/men/shirts or anything. my products controller and men method is dynamic but I cant create any segment of men. my controller is
class Products extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->model('product_model');
}
public function index(){
$data['title']='Products';
$this->load->view('templates/header',$data);
$this->load->view('products/navigator');
$this->load->view('products/index',$data);
$this->load->view('templates/footer');
}
public function woven($page='home'){
$data['woven_men']=$this->product_model->woven_men();
$data['woven_women']=$this->product_model->woven_women();
$data['woven_children']=$this->product_model->woven_children();
$data['title']= "Woven : ".ucfirst($page);
$this->load->view('templates/header',$data);
$this->load->view('products/navigator');
$this->load->view('products/woven/'.$page);
$this->load->view('templates/footer');
}
}
I know It possible to create more segment using method parameter. but how can use this inside woven method like shirt, pant, shorts etc

Related

Getting the database data to front view in laravel?

As it is quite easy to get data from database in admin panel,but the case is that how can i get/view those data to front view , front view contains the data from several table. Could you please help me ? How can i manage those front pages easily ?
There are a lot of ways, but this is what I normally do.
Use a separate controller method (say index() HomeController) for the frontpage, and create an array of all the variables with the data from database and pass it on to the page. Let's say our homepage has some slider images, some blog posts, and and our team section, then your HomeController would look something like this:
class HomeController extends Controller
{
public function index()
{
$data = []; //your empty array
$data['sliders'] = Slider::all(); //data from sliders table
$data['posts'] = Post::all(); //data from posts table
$data['teams'] = Team::all(); //data from teams table
return view('frontend.index',compact('data'));
}
Now you can access all these in your frontend blade (here index.blade.php) as such:
#foreach($data['teams'] as $key=> $value)
//your loop code
#endforeach
Hope this helps.
In the wonderfull world of laravel this is super easy. But first let me dive a into the structure of laravel a bit.
It all begins with the controller. In Laravel, Controllers a re meant to group assiociated request handling logic within a single class. A basic resource controller would look like this:
class DomainController extends Controller
{
public function index(){} // list domains
public function create(){} // show create form
public function store(Request $request){ } // handle the form POST
public function show($id){} // show a single domain
public function edit($id){} // show edit page
public function update(Request $request, $id){} // handle show edit page POST
public function destroy($id){} // delete a domain
}
Now how can you return the data to the view?
Let's say we want to generate a list of all users using the index function from the controller.
public function index()
{
$users = User::all();
return view('users.index', compact('users'));
}
With this return statement we will get a view and in that view we will have the $users variable available because of the use of the comapct function.
Now if we want to show a simple list:
#foreach($users as $user)
<li>{{$user->name}}</li>
<li>{{$user->age}}</li>
#endforeach

White Screen When posting in CodeIgniter

Codeigniter gives a white screen every time a form is posted:
Here is the controller logic [controllers/account.php]:
class Account extends CI_Controller
{
public function create()
{
if($this->input->post(NULL, TRUE)){
$params = $this->input->post();
//add validation layer
$accountOptions = array($params are used here)
$this->load->model('account/account', 'account');
$this->account->initialize($accountOptions);
$this->account->save();
}
$header['title'] = "Create Free Account";
$this->load->view('front_end/header', $header);
$this->load->view('main_content');
$content['account_form'] = $this->load->view('forms/account_form', NULL, TRUE);
$this->load->view('account/create', $content);
$footer['extraJs'] = "account";
$this->load->view('front_end/footer', $footer);
}
}
Here is the Account Model logic [models/account/account.php]:
class Account extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function initialize($options)
{
//initialize
}
}
The view first loads fine then after filling the form and clicking submit, just white page.
I tried to add __construct to the controller and load account/account from there, the form does not even load. Any ideas?
I just found the problem:
- The Model account has duplicated definition and the error_reporting was off!
You shouldn't have two classes with the same name Account (the controller and model). Check your server and/or Codeigniter log it should show up there.
I advise you to call your controller class Account and your model class M_Account. You can then rename the model to account whenever you load it, just like you did:
$this->load->model('account/m_account', 'account');
public function __construct()
{
parent::__construct();
$this->load->model("account/account");
}
you load model,library and helper files in construct dont load to inside a function

codeigniter : load second controller after execution of first controller

we have two controllers one is members.php which is registering the user another one is reserv.php for reserving a ticket for busses.
I want to load second controller after completion of first controller class.
members.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Members1 extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('member_model1');
}
public function index() {
$table = $this->member_model1->insert_members();
$data['members'] = $table;
//$this->load->view('header1', $data);
$this->load->view('reservation_view',$data);
//$this->load->view('members');
//$this->load->view('footer');
}
}
reserv.php
<?php
class reserve extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('member_model1');
}
function index() {
$table = $this->member_model1->get_members();
$data['members'] = $table;
//$this->load->view('header1', $data);
$this->load->view('reservation_view',$data);
//$this->load->view('reservation_view');
// $this->load->helper(array('form'));
//$this->load->view('reservation_view');
}
}
?>
To call a controller from another controller, you can use the function redirect() :
// In Members1 controller
redirect('/reserve/index');
Note that you won't be able to send data along with the redirect, such as when you load a view. To send data, you must use $this->session->set_flashdata(); because the redirects seem to use header() (I advise you to have a loot at this post).
// In Members1 controller
$this->session->set_flashdata('key', 'value');
redirect('/reserve/index');
By the way, when I look at your two classes, I wonder why you don't simply create a unique Ticket class with two methods like registerUser() and bookTicket() (these names are pure fancy, be free to use yours).

Constructor session validation for different functions

I'm doing a validation in my constructor which checks whether the user has started a session.
I'm then throwing in some controllers for various things.
My question is - can I have some of the controllers unaffected by the construct function(session validation), or do i have to take it into another file? The reason is, that I would like to have two sets of function in the constructor - one for when the user is logged in, and the rest when they are not.
I used to have two files and switch between them, but then i decided to combine them into one.
Thanks
What you need to do is set up a base controller that will look after the session for you and split your logged in controllers from your logged out ones via inheritance.
My suggestion for you is to have a look at Phil Sturgeon's post on Keeping It Dry. In his post, Phil explains the basics on how to implement controllers in such a way that the parent controller will look after sessions so you don't have to check it every time.
As an example:
MY_Controller:
class MY_Controller extends CI_Controller{
function __construct(){
parent::__construct();
//do things in here that ALL controllers should do.
}
}
MY_In_Controller:
class MY_In_Controller extends MY_Controller{
function __construct(){
parent::__construct();
//if user doesnt have the session redirect them out NOW!
}
}
MY_Out_Controller:
class MY_Out_Controller extends MY_Controller{
function __construct(){
parent::__construct();
//if the user has the session, redirect them in NOW.
}
}
Login Controller:
class Login extends MY_Out_Controller{
function __construct(){
parent::__construct();
}
function index(){
//folks that have a session (already logged in) will never even get here.
//because the parent class MY_Out_Controller already redirected them back in.
}
}
Manage Controller:
class Manage extends MY_In_Controller{
function __construct(){
parent::__construct();
}
function index(){
//folks that don't a session (logged out) will never even get here.
//because the parent class MY_In_Controller already redirected them out.
}
}
In short, don't write your session checks directly in controllers. You'll be writing it too often and violating the DRY Principle.
Update: I recommend revamping Phil's method by using Shane Pearson's method in CodeIgniter's Base Classes Revisited.
As stated earlier why don't you use separate different base controllers for specific purpose.
I would suggest as this way for you
let us take two part one let's say frontend controller which donot require session and another controller let's say backend controller which needs session
Now,
//application/core/Front_controller.php
class Front_Controller extends MY_Controller
{
function __construct()
{
parent::__construct();
}
}
another class under core/backend_controller
class backend_Controller extends MY_Controller
{
function __construct()
{
parent::__construct();
$this->load->library('session');
}
}
now you can extend the front_controller for those controller you do not need session and extend backend controller which requires session from application/controller

base controller and apply it to all existing controller

I need to create codeigniter base controller to check allowed ip address in database by mobel function if the ip is exists then user should go to home page but if the ip address is not exists and show 404 page in codeigniter, i can't find core folder in application folder
First, you need to extend a core class, call it MY_Controller.php
Save that file in: application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('ip_table_model');
$this->load->library('input');
// assuming there's a function called "check_ip($ip_address)" in ip_table_model
if (!$this->ip_table_model->check_ip($this->input->ip_address()) {
redirect('error_404');
}
}
}
Now, we're assuming you have a model called ip_table_model which connects to database with list of IP addresses, and there's a function called check_ip which will validate whether user has access or not. This is relatively simple, and I won't show any examples on this.
The redirect('error_404'); page does not yet exist, you need to create a controller which shows your 404 page.
Now, for any other controllers in your project, instead of extends CI_Controller, make them extend MY_Controller instead.
Here's an example:
class Welcome extends MY_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->view('welcome_message');
}
}
Explanation: We're extending CI_Controller to create our own core controller, called MY_Controller. Inside, we're checking if user has access or not through the constructor, which will be called in every other controller in the project.
References:
http://codeigniter.com/user_guide/general/core_classes.html
http://codeigniter.com/user_guide/libraries/input.html
Answer is here (section Extending Core Class).
1.7.2 has a different structure to 2.0.*, therefore there is no core folder in application
In Core Create a new Class .
Name MY_Controller.php
class MY_Controller extends CI_Controller {
// Write your functions here which you wanna use throughout the website
public function abc (){
echo "Helllo";
}
}
class Welcome extends MY_Controller {
function __construct()
{
parent::__construct();
}
function your_custom_fuctions()
{
$this->abc(); //echo Hello...
//Anything you want to do
}
}
function admin_view($view_name = "", $header_info = NULL, $sidebar_info=NULL,$page_info = NULL, $footer_info = NULL, $data_info = ""){
$this->load->view('Admin/includes/header', $header_info);
$this->load->view('Admin/includes/Left_sidebar', $sidebar_info);
$this->load->view($view_name, $page_info);
$this->load->view('common/footer', $footer_info);
}

Resources