How to Include/Use Composer Bootstrap in CodeIgniter? - php

I want to include/use bootstrap in all my controllers/views without having to load each css/js in each view file, so I have done this:
Installed Composer Bootstrap
composer require twbs/bootstrap
My Index Controller:
public function index() {
// Composer Autoloader
require VENDORPATH.'autoload.php';
require_once BASEPATH.'core/CodeIgniter.php';
echo '<div class="section jumbotron text-center">Yu in index son.</div>';
}
VENDORPATH = myhomefolder../vendor/
vendor/autoload.php
// autoload.php #generated by Composer
require_once __DIR__ . '/composer' . '/autoload_real.php';
return ComposerAutoloaderInit9d54f40b1177ed0ebd8d1d378ec06d06::getLoader();
/composer.json
{
"require": {
"twbs/bootstrap": "^3.3"
}
}
I don't know what to do now, I have searched all the web but it only says things about other packages or something not related and Im stuck in this right now, if someone in advance can help, I would appreciate, thank you.

First
create header and footer which consists of bootstrap css and js
and put them into a templates folder in your views folder
In controller use them like this to every method:
public function index() {
$data['title'] = 'home';
$this->load->view('templates/bootstrap_header',$data);
$this->load->view('index',$data);
$this->load->view('templates/bootstrap_footer');
}

What I do, is that I use a "main" template, which includes the css, js, head, body, etc... Then, inside my body tag, I do the $this->load->view('folder/function', $data); which processes the internal view... Do I make sense ???
Main View:
<html>
<head>
<?php $this->load->view('html/head', $view_data); ?>
</head>
<body lang="es">
<div class="container">
<?php $this->load->view('html/header')?>
<div class="contenido">
<?php if (isset($which_view)) $this->load->view($which_view, $view_data)?>
</div>
<?php $this->load->view('html/footer');?>
</div>
</body>
</html>
And finally, I create a small "inner-view" in a folder named after my controller, named after my function , so everything stays in sync...
<div class="formatoLogin modulo">
<?php echo form_open('login/doLogin', array('id'=>'formLogin', 'class'=>'form-signin'))?>
<?php $this->load->view('mod/notificacion')?>
<h1>Acceso Restringido</h1>
<div class="form-group">
<label for="Usuario" class="control-label">Usuario:</label>
<input type="text" name="Usuario" id="Usuario" class="form-control" />
</div>
<div class="form-group">
<label for="Contrasenia" class="control-label">ContraseƱa:</label>
<input type="password" name="Contrasenia" id="Contrasenia" class="form-control">
</div>
<div class="form-group text-center">
<button class="btn btn-info">Acceder</button>
</div>
<?php if($this->uri->segment(3)=='redirect'){?>
<input type="hidden" name="redirect" value="<?php echo $this->uri->segment(4).'/' . $this->uri->segment(5).'/' . $this->uri->segment(6) ?>">
<?php } ?>
<?php echo form_close()?>
</div>
Do I make sense? I hope it works cause it's saved me tons of includes...

Related

Require_once not working, can't call php function

My project has an index.php file, and a utils.php file. I am trying to call a function in the utils.php file called dropdown. This function is going to generate a dropdown menu (I'm using Bootstrap to do that).
So, here is my index.php file:
<div class='container panel panel-default'>
<div class='panel-body'>
<!-- This is where I call the function to generate a dropdown -->
<?php require_once('utils.php'); dropdown(); ?>
</div>
</div>
And here is the utils.php file, containing the dropdown() function:
<?php
function dropdown () {
$str = "<form>
<div class='form-group'>
<label for='sel1'><h3>Pick a Year below:</h3></label>
<select class='form-control' id='sel1'>
<option>2016</option>
<option>2014</option>
<option>2013</option>
<option>2012</option>
</select>
</div>
</form>"
echo $str;
}?>
I am doing it this way, because in the future I'll have the dropdown options to be dynamic, using a database. Thanks everyone! ;)

calling a different classes function via a form php

We are toying about with the Open Source Flamer app. We noticed that they are able to call a function in a separate class located in a different PHP file via a form POST request. Here is an example of how they do it:
<div id="upldate_location_service" class="div_service">
<form action="process.php/upDetails" method="post">
<h3>Update location :</h3>
<div class="form_row">
<div class="form_label">Session Token *: </div>
<div class="form_field"><input type="text" name="ent_sess_token" />name= "ent_sess_token"</div>
</div>
<div class="form_row">
<div class="form_label"> </div>
<div class="form_field"><input type="submit" name="ent_submit" value="Submit" /></div>
</div>
</form>
</div>
The function they are trying to access is UpdateLocation in the file Process.php. We have tried to recreate this process but it wont work. We can echo outside of our class, but for some reason we cant access or run any content in our process.php function.
Here is a copy of our process document:
class LegitAPI Extends API{
public function upDetails($args)
{
echo "Hello World";
}
}
Theoretically, when submitting the form on the index page, it should load process.php and then the method upDetails in the LegitAPI Class and display hello world. However, a blank page is instead being displayed.
Any ideas?

CodeIgniter controller/view dilemma

I have created a simple register/login system and it (I think) works well so far.
Now I want to implement some of it's functions to my site.
controllers/main.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Main extends CI_Controller
{
public function index()
{
$this->home();
}
public function home()
{
$this->load->view('home.php');
}
}
views/home.php:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Home</title>
</head>
<body>
<div id="container">
<?php if ($this->session->userdata('is_logged_in')): ?>
logged in as: <?php echo $this->session->userdata('username'); ?></br>
Logout
<?php else: ?>
not logged in</br></br>
<?php echo form_open('login'); ?>
<?php echo form_error('username'); ?>
<input type="text" name="username" placeholder="Login" />
</br>
<?php echo form_error('password'); ?>
<input type="password" name="password" placeholder="Password" />
</br></br>
<div><input type="submit" value="Sign in" /></div>
<?php echo form_error('login_validation'); ?>
</form>
<?php endif ?>
</div>
</body>
</html>
Is this the right way of doing things in CI?
For controller part instead of using the home(), you can directly load the view in index() because internally you are navigating from index to home() without any specific reason.
Also the loading view doesn't require ".php" extension in your file and you can do like this:
$this->load->view('home');
Where home.php is the file located inside application/views/
For showing the error messages, you can use another optimal way by defining the validation rules(in array fromat) inside config/form_validation.php and just require below code to show all messages instead one by one
<?php echo validation_errors()?>

How to Call PHP Function in a Class from HTML Button Click

I am a rookie PHP developer.
I have a PHP web project with an HTML page that contains an Add button. The name of the page is Awards.html. Elsewhere I have created a PHP class, Awards.php which contains a function.
The source code of my files is given as follows:
Awards.html
<div class="divparent">
<div class="modal-header">
<div class="btn-group">
<button class="btn" data-bind="click: closeModal">Exit</button>
</div>
</div>
<div class="modal-title">
<h1 id="headerid">Awards</h1>
</div>
<div class="modal-body">
<input id="hdnValueCurrentAwardSoid" type="hidden" value="null" />
<div class="divleft">
<input id="txtName" maxlength="80" type="text" class="water newpost1" placeholder="Award Name" tabindex="1" />
<section class="ThumbnailContainer">
<img id="imgThumbnail" class="imgThumbnail" />
<img src="http://localhost/rockontechnologies/Images/GreenRibbon.png" id="pictureribbon" class="pictureribbon" />
<input type="text" contenteditable="false" readonly id="transformtext" />
</section>
<textarea id="txtdescription" placeholder="Description" class="water newpost1" rows="4" tabindex="2"></textarea>
<div class="ui-widget">
<input id="txtIssueOrg" maxlength="50" type="text" placeholder="Issue Organization" />
</div>
</div>
</div>
<div class = "divbottom">
<div id="divAddAward">
<button class="btn" onclick="">Add</button>
</div>
</div>
</div>
Awards.php
<?php
class Awards
{
function clickFunction()
{
//Function that needs to be executed!
}
}
The problem here is that on the click event of the Add button, I need to call the clickFunction() of the Awards.php file. Can anyone please tell me how to do this?
Replies at the earliest will be highly appreciated. Thank you.
You should do something like that
<button class="btn" onclick="onrequest();">Add</button>
function onrequest() {
$.post(
'example.php'
).success(function(resp){
json = $.parseJSON(resp);
alert(json);
});
}
and in example.php call your function
$class = new Awards();
$method = $class->clickFunction();
echo json_encode($method);
and in your clickFunction();
clickFunction(){
$array = array(
'status' => '1'
);
return $array;
}
first of you have to a instance of class which is like that
$class = new Awards();
then if you want to onclick event you should make a javascript code but how to get the function you can do like this
<?php echo $class->clickFunction(); ?>
I think you can't call a class directly from HTML in PHP you have to call it through HTTP by loading a page.
Hence, use a AJAX call to call Awards.php. Eg. with JQuery it could look like this $.get("Awards.php", function() {
alert( "success" );
})
In your php-file you should also call your class something like this <?php echo $class->clickFunction(); ?>

creating a dynamic sidebar with zend framework

I am trying to create a dynamic sidebar in zend framework. I have googled some articles, browsed even the stackoverflow archive but i can't seem to get it so please help me figure this out.
Here is some code from my layout.phtml file:
<div id="contentWrapper">
<div id="contentArea">
<?php echo $this->layout()->content;
?>
</div>
<div id="sidebar">
<div id="user-authentication">
<?php if (Zend_Auth::getInstance()->hasIdentity()) {
?>Logged In as<br />
<?php
echo Zend_Auth::getInstance()->getIdentity();
} else {
?>
<input type="text" name="login" class="loginInput" /><br/>
<input type="password" name="password" class="loginInput" /><br/>
<input type="submit" name="submit" value="Log In" class="loginButton" />
<?php } ?>
</div>
<div id="sidebar-content">
<? echo $this->layout()->sidebar; ?>
</div>
</div>
</div>
I could use this Best practice creating dynamic sidebar with zend framework but that means I would need to have redundant code for displaying the login box/logged in as.
Are you just worried about the repetition of hasIdentity and getIdentity in the sidebar-content div?
If so, maybe you'd prefer this:
<?php
$auth = Zend_Auth::getInstance();
$user = $auth->hasIdentity() ? $auth->getIdentity : NULL;
?>
<div id="user-authentication">
<?php if ($user) { ?>
User stuff.
<?php } else { ?>
Public stuff.
<?php } ?>
</div>
<div id="user-authentication">
<?php if ($user) { ?>
User stuff.
<?php } else { ?>
Public stuff.
<?php } ?>
</div>
However, that's just a matter of coding style. There's nothing wrong with checking a users logged in state more than once. It's necessary with modular code to do so; that's probably why Zend_Auth is a Singleton.
Looks like you need a widget:
Using Action Helpers To Implement Re-Usable Widgets - phly, boy, phly
For displaying or not some of the content, look for integration of Zend_Acl and Zend_Navigation.
It is as easy as: $container->setAcl($acl)

Categories