Setting up a full site page in CodeIgniter - php

I am just starting with CodeIgniter, but I can’t quite figure how to sort out my views.
I’ve made a sample layout explaining my problem, and is attached to this post.
I have a menu box, a user-system box and a content box.
In procedural PHP I would have a page called index.php with a parameter from GET, including the content. The user-system would just be included in the index.php file inside the box, and so would the menu.
How can I do this in a proper way using MVC and CodeIgniter?

Try Most Simple Template Library for CodeIgniter. It allows you to do what you would do in your php example. Create a "main" view and channel your content to it using your controller(s). You can create "subthemes" for body, content, sidebars etc.

Hope you have configured codeigniter on your machine.
now create one file in controller folder
for example :
D:\wamp\www\demoProject\application\controllers\homePage.php
add following code in homePage controller
<?php
class HomePage extends CI_Controller {
var $controller = "homePage";
var $viewContent = array();
function list_homePage() {
// Load view pages
// Load header view page
$this->load->view('xome/header');
// Load main view page
$this->load->view('xome/list_' . $this->controller,
$this->viewContent);
// Load footer view page
$this->load->view('xome/footer');
}
}
?>
after that create one file in view folder
for example :
D:\wamp\www\demoProject\application\views\list_homePage.php
put your html code in view file.
Hope this will help you... :)

For menu I would create a model like this:
class mMenu extends CI_Model{
function mMenu(){
parent::__construct();
}
function home(){
$menu = array(
'main_menu' => '<ul>
<li>Menu Link 1</li>
<li>Menu Link 2</li>
<li>Menu Link 3</li>
</ul>'
);
return $menu;
}
}
In controller, say home:
function home(){
$this->load->model('mMenu');
$options['menu'] = $this->mMenu->home();
$this->load->view('home_view', $options);
}
In the view file, wherever I need the menu to load:
echo $menu['main_menu'];
You should do something similar to control the state of the user and return the form if the user is not logged in and anything else if the user is logged in.

Related

PHP - how to link other pages in view in ci

I am new to CodeIgniter. I am using a bootstrap template. welcome_page.php is my view file. I have tried to link my home button to redirect to another page that is 'dashboard.php' which is in view folder of my project. I am getting an error "_the access is forbidden_ or _object is not found_".
Can anyone tell me how to link pages with an example. Thanks for the help.
controller file code:
public function home()
{
$this->load->view('dashboard');
}
view file code:
<li class="nav-item active">
<a class="nav-link" href="<?php echo BASE_PATH . "views/dashboard.php";?>">Home
<span class="sr-only">(current)</span>
</a>
</li>
The entry point for any MVC framework is the controller and you cannot access views directly.
In CodeIgniter, the url is composed of controller name and action name (public method in controller).
For example if you want the url
localhost/index.php/welcome/dashboard. You need to have a controller class called Welcome and a public method in that controller called dashboard. You can display the view by putting the command $this->load->view('welcome_message'); in function dashboard.
Url format:
<Your project root>/index.php/<small caps of the controller name>/<name of the public function in the controller>
All of this means that you just need to add function dashboard to your controller named Welcome.
Your controller should look like this.
public function home()
{
$this->load->view('dashboard');
}
public function dashboard()
{
$this->load->view('welcome_page');
}
Now you can access url localhost/index.php/welcome/dashboard
You can read more in their tutorial
https://codeigniter.com/user_guide/tutorial/static_pages.html#static-pages

How to route, fetch and display data from two tables in db to a single webpage in laravel 5.2

im new to laravel and i am currently doing a study project in laravel 5.2. Thr project is just a simple website having 3 pages (Home, About Us, Contact Us) fetching each page content from database and the home page having a slideshow. For that i downloaded a free responsive html template and divided it into header.blade.php, slideshow.blade.php,footer.blade.php and stored it inside a folder named includes under view folder. And the main layout file default.blade.php is stored inside a folder named layouts under view folder. I included these header.blade.php, slideshow.blade.php and footer.blade.php in the main layout file default.blade.php to form the full web template.
My default.blade.php file code is as given below:
<!DOCTYPE html>
<head>
#include('guest.includes.head')
</head>
<body>
<div class="wrap-body">
<header>
#include('guest.includes.header')
</header>
#if(Request::is('/'))
#include('guest.includes.slideshow')
#endif
#yield('content')
<footer class="zerogrid">
#include('guest.includes.footer')
</footer>
</div>
</body>
</html>
Inside the header.blade.php i had some information like phone num, email, website address that are fetched from database and displaying that data on all my 3 pages along with each page contents from db. For that i used share() function inside boot method of AppServiceProvider.php to share and display my header data on all my webpages.
and my current slideshow.blade.php code is like as given below:
<div class="zerogrid">
<div class="callbacks_container">
<ul class="rslides" id="slider4">
<li>
<img src="assets/images/banner1.jpg" alt="">
<div class="caption">
<h2>Title 1</h2></br>
<p>Description 1</p>
</div>
</li>
<li>
<img src="assets/images/banner2.jpg" alt="">
<div class="caption">
<h2>Title 2</h2></br>
<p>Description 2</p>
</div>
</li>
</ul>
</div>
</div>
Here you can see that i had hard-coded the slideshows slide item details(slideimage, title, description) above. Upto this part is working fine and i am getting header data from db to server via header.blade.php to all pages, each page contents from db via Routes/Controllers and the above said slideshow.
My routes.php code is as given below:
Route::get('/', 'GuestController#home');
Route::get('aboutus', 'GuestController#aboutus');
Route::get('contactus', 'GuestController#contactus');
My Controller Page(GuestController.php) code is as given below:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class GuestController extends Controller
{
public function home() {
$result=DB::table('contents')->where('menuname','home')->get();
return view('guest.home')->with('data',$result);
}
public function aboutus() {
$result=DB::table('contents')->where('menuname','aboutus')->get();
return view('guest.aboutus')->with('data',$result);
}
public function contactus() {
$result=DB::table('contents')->where('menuname','contactus')->get();
return view('guest.contactus')->with('data',$result);
}
}
Now i want to make the slideshow also to be dynamic. I mean to fetch the slide item details (slideimage, title, description) also from db itself. For that i created a table named slideshow with fields slideimage, title, description. But i dont know how to fetch and display data from two independant tables (slideshow & contents) in db to a single web page.
(Here in my case actually in my home page it will fetch 3 sets of data from db. first set of data is header data from headers table that will display header data like phone, email, website etc to all my pages including homepage via share() function and the second set of data is the home page contents from content table via routes/controller pages. These two are already working and now iwant to fetch data for slideshow also from slideshow table in db which is my 3rd set of data to be display on homepage.)
Any solution for this requirement? Please Help! Thanks in advance...
When i searched for various solutions for my requirement as asked in my above question, i found a simple method of satisfying this requirement of fetching and displaying datas from two tables inside a single view can be done by just modifying the home() method in the above given controller page code ( GuestController.php ) as given below:
public function home() {
$homecontent=DB::table('contents')->where('menuname','home')->get();
$slideshow=DB::table('slideshow')->get();
return view('guest.home')->with('homecontent',$homecontent)->with('slideshow',$slideshow);
}
Then inside the slideshow.blade.php loop the result from slideshow variable like:
<ul class="rslides" id="slider4">
#foreach($slideshow as $row)
<li>
<img src="assets/images/{{$row->slideimage}}" alt="">
<div class="caption">
<h2>{{$row->title}}</h2></br>
<p>{{$row->description}}</p>
</div>
</li>
#endforeach
</ul>
and same like slideshow, loop the contents of the homecontent variable inside the homepage file something like:
#foreach($homecontent as $row)
<article>
<div class="art-header">
<h3>{{$row->contenttitle}}</h3>
</div>
<div class="art-content">
<img src="assets/images/{{$row->contentimage}}" />
<p align="justify">{{$row->contentdescription}}</p>
</div>
</article>
#endforeach
No other changes required in any other pages and it will display slideshow and homepage contents inside the homepage itself along with the shared header datas from header table on all pages including homepage.
You have answered your own question and you should probably select it as a answer for this thread.
(Comments are too short, that's why I am writing you an answer)
Since you said you are teaching yourself Laravel I would strongly recommend you watch this course. It is a free one and it seems to help people a lot. It certainly helped me too although I had previous experience with the framework.
Anyway you know how you write a project and then you come one year later and think "What the .. was I thinking when I wrote this?" So I believe we should name our things clearly and precisely just for the sake of saving our future selves' souls.
I hope you take this as a positive critique and not as bashing on you. With all respect to you I am just trying to convey some wisdom here...
What do I mean exactly:
1) you named your controller GuestController. But you have no guests table. Probably you don't have a users table too. So where are the guests? Your controller should be much rather named something along the lines of HomeController or FrontController or even split into may different controllers.
"NewsController"
The basic rule is "one table - one model - one controller" so first of all you should probably not keep your articles in the same table as your pages contents. What an experienced developer would do is have two separate tables for those. So probably NewsController for the news, a News model and a news table. Then another controller for the pages table, a Page model and a PagesController.
2)
"PagesController"
Here you can put your home and about pages.
so
class PagesController {
public function home(){ } //will show your slideshow and news feed
public function about() { } //will show the 'about' content from `contents`/`articles` table
}
then another one
class NewsController(){
public function show($id) { } //will show a single `news` article
}
depending on your site architecture you can
- have the slides editable trough the front part of the site or have an /admin/ directory with another set of controllers where you have another SlideController and a NewsController where you can add and edit news and slides
- or add controllers to the front part of the site to add/edit news and slides
Hope it helps along your way!
Regards
I recommend using Eloquent ORM
The model is made so
namespace App;
use Illuminate\Database\Eloquent\Model;
class Slideshow extends Model
{
protected $table='slideshow';
}
Then the controller so writing
namespace App\Http\Controllers;
use App\Slideshow;
use App\Contents;
public function __construct(Contents $contents)
{
$contents=$contents->get();
view()->share('data', $contents);
}
class GuestController extends Controller
{
public function home() {
$slideshows=Slideshow::where('menuname','home')->get();
return view('guest.home', [ 'slideshows'=>$slideshows]);
}
public function aboutus() {
return view('guest.aboutus', []);
}
public function contactus() {
return view('guest.contactus', []);
}
P.S. Do not forget to register contents model

"Action" in indexController not rendering in view

I have an existing Zend 1.12 project with modules et al that I am trying to simply add a view to create another web page. So there is a controller called "Management" that has an index action, the default of course, and the correct view page called index.phtml. That is all fine and works inside the entire project/website.
class Management_IndexController extends Default_Controller_Action {
function indexAction() {
$this->view->headStyle()->appendFile('contact.css', 'contact')
->appendFile('message.css')
;
$this->setLayout('management');
$this->view->headTitle()->set('Management');
$message = new Com_Ui_Message();
$request = $this->_request;
$settings=new Com_Data();
$content=new Com_Data();
$data = new Com_Data($request->getPost());
$this->setFocus('#name');
}
function chatAction() {
$this->view->headStyle()->appendFile('contact.css', 'contact')
->appendFile('message.css')
;
$this->view->testMessage = "test";
}
}
So if I browse to http://www.bmcmusicgroup.com/management I see the default action public function, and that is all good. If I try to add another public function called chatAction inside of the management controller, and create a view page called chat.phtml in the appropriate folder where the index.phtml file is (the management index.phtml file), and then point my browser to http://www.bmcmusicgroup.com/management/chat I get nothing.
This is an existing site that I am trying decipher. Any pointers would be greatly appreciated.
Try changing your url to the following and you should see the page: http://www.bmcmusicgroup.com/management/index/chat. The URL you entered (http://www.bmcmusicgroup.com/management/chat) decomposes like so:
Module: management;
Controller: chat;
Action: index

how to call a view file from controller(cakePHP)

I am learning cakephp from few days ... so please help a bit thanks .
I made a controller --
class PostController extends AppController {
var $name='Posts';
function index(){
$posts=$this->Post->find('all');
$this->set(compact('posts'));
}
in view had post folder with index.ctp file . I want to ask from where it gets "CakePHP: the rapid development php framework" and from where it takes content . I send this data from controller so it prints only var_dump($posts); ... Thanks in advance.
cake php has default layout files in folder "app/view/Layout" .. file name default.ctp in this folder will be taken as default ..
if you open default.ctp in layout you will see something like
<h1><?php echo $this->Html->link($cakeDescription, 'http://cakephp.org'); ?></h1>
and other links defined ..you can comment the and check what changes you get in view.
here is from where view gets ""CakePHP: the rapid development php framework"
this line
<?php echo $this->fetch('content'); ?>
in layout fetch the data from controller to show in view which we set using $this->set() in controller
if you want to change the layout create your own in layout folder
and use in contoller like
class PostController extends AppController {
var $name='Posts';
function index(){
$posts=$this->Post->find('all');
$this->set(compact('posts'));
$this->layout = false; // ot you can set ypur own file like 'xyz' for 'xyz.ctp'
}
to set layout for each action in controller use
function beforeFilter() {
parent::beforeFilter();
$this->layout = 'layout';
}
To learn more about layout see http://book.cakephp.org/2.0/en/views.html#layouts
Hope you got me
"CakePHP: the rapid development php framework"
For above look inside views -> layout -> default.ctp file.
Also change default cakephp routing so you can see posts index action when you have posts in URL.
The best way to call a layout from controller is
$this->layout = 'Your layout page name';

Load another view when click on a image

I am using codeigniter for a project. I set controller A as my default controller in routes.php. So in this controller A it will load my home page.
If I want to access another view from the homepage for example about or contact, how do I go about doing it??
Example:
<a href="http://yoursite.com/yourcontroller/yourfunction">About Page
Now you have yourcontroller that has the yourfunction in it, from within this function you can load the view for about page, like:
function yourfunction() {
$this->load->view('your_about_view_page_name");
}
And that shall work. Hope it helps

Categories