How to give link to a file in subfolder in codeigniter - codeigniter

This is my page in View.
<!DOCTYPE html>
<?php $this->load->view('partials/page_head');?>
<body>
<div class="wrapper">
<header>
<div class="logo">Logo</div>
</header>
<?php $this->load->view('partials/menu');?>
<div id="content">
<?php $this->load->view('partials/', $subview); ?>
</div>
<footer>© 2012 Codeigniter.tv</footer>
</div>
<script>document.write('<script src="http://' + (location.host || 'localhost').split(':')[0] + ':35729/livereload.js?snipver=1"></' + 'script>')</script>
</body>
</html>
This is my menu page inside partials folder.
<menu>
<ul>
<li>Listing</li>
<li>Detail</li>
</ul>
</menu>
Below is the controller part.
<?php
class Example extends CI_Controller {
public $data = array('subview' => 'Oops, forgot to set a subview');
public function __construct(){
parent::__construct();
$this->load->helper('url');
}
public function listing() {
$this->data['subview'] = 'listing';
$this->load->view('layouts/layout', $this->data);
}
public function detail() {
$this->data['subview'] = 'detail';
$this->load->view('layouts/layout', $this->data);
}
}
I could not display the content in the file 'listing.php' which is in partials folder. When i run the project in localhost it saying not found. Can anyone help me to correct my problems.

modify views as bellow.
<!DOCTYPE html>
<?php $this->load->view('partials/page_head');?>
<body>
<div class="wrapper">
<header>
<div class="logo">Logo</div>
</header>
<?php $this->load->view('partials/menu');?>
<div id="content">
<?php $this->load->view('partials/'.$subview); ?>
</div>
<footer>© 2012 Codeigniter.tv</footer>
</div>
<script>document.write('<script src="http://' + (location.host || 'localhost').split(':')[0] + ':35729/livereload.js?snipver=1"></' + 'script>') </script>
</body>
</html>

modify your menu file as below
<menu>
<ul>
<li>Listing</li>
<li>Detail</li>
</ul>
</menu>

Related

Pass data from view to view and display in view laravel?

In Controller :
$data = [
'name' => 'Jony',
'age'. => 18
];
return View::make("user/detail", compact('data'));
In view/user/detail.blade.php
<!DOCTYPE html>
<html lang="en-EN">
<head>
<meta charset="utf-8">
</head>
<body>
<h1>Details</h1>
<div class="user-informatio">
//
I want to display view : user.blade.php
//
</div>
</body>
</html>
In view/user/user.blade.php
<div>{{ $data['name'] }}</div> <br/>
<div>{{ $data['age'] }}</div>
I want to pass $data from detail.blade.php to user.blade.php and display user.blade form inside detail.blade.
Please give me any ideas.Thanks
You can #include user.blade.php file in it.
<div class="user-informatio">
//
I want to display view : user.blade.php
//
#include('user.user', $data)
</div>
try this
return view('user/detail')->with('data', $data);
then in the view just use $data

view to view in HMVC Code Igniter

i have some problem with hmvc in codeigniter because im newbie with hmvc.
i have a template bootstrap with ci , so the problem like i not able to show my view
my view name index.php in directory "theme" :
<!DOCTYPE html>
<html lang="en">
<head>
<?php echo $this->load->view('theme/header') ?>
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<?php echo $this->load->view('theme/navbar') ?>
</nav>
<div id="wrapper">
<div id="page-wrapper">
<?php $this->load->view($content) ?>
</div>
</div>
<?php echo $this->load->view('theme/footer') ?>
</body>
My Controller name dashboard in directory "theme" :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Dashboard extends CI_Controller{
function __construct()
{
parent::__construct();
}
function index()
{
$data['content'] = 'dashboard';
return $this->load->view('theme/index', $data);
}
}
i the error is in :
<?php echo $this->load->view('theme/navbar') ?>
and
<?php echo $this->load->view('theme/footer') ?>
the error Says :
"A PHP Error was encountered"
"Severity: 4096"
"Message: Object of class MY_Loader could not be converted to string"
"Filename: theme/index.php"
"Line Number : 8"
Please help
dont use echo
just $this->load->view('viewfile')
example if your view file is on application/view/theme/footer then it is
$this->load->view('theme/footer')
if you want to convert it to string you can pass a boolean true in Third parameter$this->load->view('theme/footer',array(),true)
Please refer to this codeigniter's view's documentation

Getting error on laravel AppServiceProvider

I'm learning laravel 5.2 and trying to make a cms project by following a video tutorial. I have created the files as follows-
app/View/Composers/InjectPages.php
namespace App\View\Composers;
use App\Page;
use Illuminate\View\View;
class InjectPages
{
protected $pages;
public function __construct(Page $pages)
{
$this->pages = $pages;
}
public function compose(View $view)
{
$pages = $this->pages->all()->toHierarchy();
$view->with('pages', $pages);
}
}
app/Providers/AppServiceProvider.php
public function boot()
{
$this->app['view']->composer('layouts.frontend', Composers\InjectPages::class);
}
resources/views/welcome.blade.php
#extends('layouts.frontend')
#section('title', 'Welcome')
#section('heading', 'This is a heading')
#section('content')
<h1>Hello World</h1>
#endsection
public/themes/views/layouts/frontend.blade.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>#yield('title') — Tuts24.com</title>
<link rel="stylesheet" type="text/css" href="{{ theme('css/frontend.css') }}">
</head>
<body>
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand">
<img src="{{ theme('images/logo.png') }}" alt="Tuts24.com">
</a>
</div>
<ul class="nav navbar-nav">
#include('partials.navigation')
</ul>
</div>
</nav>
<div class="container">
<div class="row">
<div class="col-md-12">
#yield('content')
</div>
</div>
</div>
</body>
</html>
public/themes/views/partials/navigation.blade.php
#foreach($pages as $page)
<li class="{{ Request::is($page->uri_wildcard) ? 'active' : '' }} {{ count($page->children) ? ($page->isChild() ? 'dropdown-submenu' : 'dropdown') : '' }}">
<a href="{{ url($page->uri) }}">
{{ $page->title }}
#if(count($page->children))
<span class="caret {{ $page->child() ? 'right' : '' }}"></span>
#endif
</a>
#if(count($page->children))
<ul class="dropdown-menu">
#include('partials.navigation', ['pages' => $page->children])
</ul>
#endif
</li>
#endforeach
But always getting the following error.
ErrorException in Container.php line 734:
Class App\Providers\Composers\InjectPages does not exist (View: /path/to/project/resources/views/welcome.blade.php)
I'm not sure, is these informations are sufficient to find out the error for you. If needed more information please let me know.
Looking forward to your response. Thanks.
Updates-
After several tries as answer it seems to me the following file is also related to this error. That's why I'm adding this code also and sharing a dropbox link of my learning project.
app/View/ThemeViewFinder.php
<?php
namespace App\View;
use Illuminate\View\FileViewFinder;
class ThemeViewFinder extends FileViewFinder
{
protected $activeTheme;
protected $basePath;
public function setBasePath($path)
{
$this->basePath = $path;
}
public function setActiveTheme($theme)
{
$this->activeTheme = $theme;
array_unshift($this->paths, $this->basePath.'/'.$theme.'/views');
}
}
Dropbox link
https://www.dropbox.com/sh/v8n1qvix20hywmo/AABVcs8DqvEhI89lw98mbxbya?dl=0
Try this:
composer('layouts.frontend', '\App\View\Composers\InjectPages::class');
Also, try to run composer dumpauto command.
Check it out
composer('layouts.frontend', '\App\View\Composers\InjectPages');
or
composer('layouts.frontend', \App\View\Composers\InjectPages::class);
Try by adding below code into composer.json
composer.json
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
},
"files":[
"app/View/Composers/InjectPages.php"
]
},
Do : composer dump-autoload
in : app/Providers/AppServiceProvider.php
public function boot()
{
view()->composer(
'layouts.frontend', 'App\View\Composers\InjectPages'
);
}

UI Bootstrap and Laravel Blade Templating

I am using UI Bootstrap in my project : UI Bootstrap
Problem : I cannot handle javascript <script> tags with blade templating. Getting internal server error (500)
Here's my Views path :
Views
includes
layouts
pages
Here's my includes/head.blade.php file :
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="">
<meta name="author" content="Scotch">
<title>Title</title>
{{ HTML::style('../public/css/bootstrap.min.css') }}
{{ HTML::script('//public/js/jquery-1.11.1.js')}}
Here's my layouts/home.blade.php file :
<!doctype html>
<html ng-app="ui.bootstrap.demo">
<head>
#include('includes.head')
{{ HTML::script('//public/js/ui.bootstrap.min.js') }}
{{ HTML::script('//public/js/angular.min.js')}}
{{ HTML::script('//public/js/example.js')}}
</head>
<body>
<div class="container">
<header class="row">
#include('includes.header')
</header>
<div id="main" class="row">
#yield('content')
</div>
<footer class="row">
#include('includes.footer')
</footer>
</div>
</body>
</html>
Here's my pages/home.blade.php file :
#extends('layouts.home')
#section('content')
<div ng-controller="ModalDemoCtrl">
<script type="text/ng-template" id="myModalContent.html">
<div class="modal-header">
<h3 class="modal-title">I'm a modal!</h3>
</div>
<div class="modal-body">
<ul>
<li ng-repeat="item in items">
<a ng-click="selected.item = item">{{ item }}</a>
</li>
</ul>
Selected: <b>{{ selected.item }}</b>
</div>
<div class="modal-footer">
<button class="btn btn-primary" ng-click="ok()">OK</button>
<button class="btn btn-warning" ng-click="cancel()">Cancel</button>
</div>
</script>
<button class="btn btn-default" ng-click="open()">Open me!</button>
<button class="btn btn-default" ng-click="open('lg')">Large modal</button>
<button class="btn btn-default" ng-click="open('sm')">Small modal</button>
<div ng-show="selected">Selection from a modal: {{ selected }}</div>
</div>
#stop
Here's my public/js/example.js file :
angular.module('ui.bootstrap.demo', ['ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('ModalDemoCtrl', function ($scope, $modal, $log) {
$scope.items = ['item1', 'item2', 'item3'];
$scope.open = function (size) {
var modalInstance = $modal.open({
templateUrl: 'myModalContent.html',
controller: 'ModalInstanceCtrl',
size: size,
resolve: {
items: function () {
return $scope.items;
}
}
});
modalInstance.result.then(function (selectedItem) {
$scope.selected = selectedItem;
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
};
});
// Please note that $modalInstance represents a modal window (instance) dependency.
// It is not the same as the $modal service used above.
angular.module('ui.bootstrap.demo').controller('ModalInstanceCtrl', function ($scope, $modalInstance, items) {
$scope.items = items;
$scope.selected = {
item: $scope.items[0]
};
$scope.ok = function () {
$modalInstance.close($scope.selected.item);
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
});
Here's my ui.bootstrap.min.js injection :
Now here's the problem :
I think in pages/home.blade.php file, i am having problem codes between <script></script> tags.
Check this out :
And here's the error i get (Sorry, the default language is Turkish)
As like i said, my purpose is using both UI Bootstrap and laravel blade templating.
Thanks in advance !
It's a little unclear if this is your only issue, but both Angular and Blade templates use the same tokens for denoting the begin / end of interpolated areas, i.e. {{ and }}. If you want to use them together, you will need to change one of them to something else, otherwise your angular code will be interpreted by Blade on the server and cause a 500 server error.
In Blade this looks like:
Blade::setContentTags('<%', '%>');
Blade::setEscapedContentTags('<%%', '%%>');
In Angular, it would look like this:
var myApp = angular.module('myApp', [], function($interpolateProvider) {
$interpolateProvider.startSymbol('<%');
$interpolateProvider.endSymbol('%>');
});

How to create Master Page(Layout) with base design style

I'm new in CodeIgniter. I want to create Master Page or Layout with base style that will be contain Menu, footer and etc. I don't want to write repeating content in all pages and load it automatically for all pages. For example, I can create Master Page in asp.net or Layout in asp.net mvc. I'm sure I can do it in CodeIgniter.
lets assume you have an html page
<html>
<head>
<title> Hello World </title>
</head>
<body>
<div id="menu">
<li>Home</li>
<li>About</li>
<li>Contact</li>
</div>
<div id="main-content">
<!-- this is the dynamic part -->
</div>
<div id="footer">
Copy Right 2013 Hello World
</div>
</body>
</html>
you could split it into
1- header
2- menu
3- main content
4- footer
you basically put
<html>
<head>
<title> Hello World </title>
</head>
<body>
in one view called "view_header"
then you put
<div id="menu">
<li>Home</li>
<li>About</li>
<li>Contact</li>
</div>
<div id="main-content">
in a view called "view_menu"
and then you put
</div>
<div id="footer">
Copy Right 2013 Hello World
</div>
</body>
</html>
in a view called "view_footer"
then in your controller
$this->load->view('view_header');
$this->load->view('view_menu');
$this->load->view('YOUR_VIEW');
$this->load->view('view_footer');
The other solution, which I see is better: create a view called view_template.php
<html>
<head>
<title> Hello World </title>
</head>
<body>
<div id="menu">
<li>Home</li>
<li>About</li>
<li>Contact</li>
</div>
<div id="main-content">
<?php $this->load->view($content); ?>
</div>
<div id="footer">
Copy Right 2013 Hello World
</div>
</body>
</html>
in the controller lets say you want to call a view called About
$data = array('content'=>'about');
$this->load->view('view_template',$data);
Dynamic Layout
You would create a new Controller with a public $template variable
Your extended Controller will then inherit the $template variable from the Master Controller.
MY_Controller
class MY_Controller extends CI_Controller
{
public $template=null;
public function __construct()
{
if(is_null($this->template)){
$this->template = 'layouts/default';
}
}
}
Admin_controller
class Admin_Controller extends MY_Controller
{
public function __construct()
{
//Still inherits from MY_Controller
//this time, any controller extending Admin_Controller
//will point to 'views/layouts/admin'
if(is_null($this->template)){
$this->template = 'layouts/admin';
}
}
}
-
class User extends MY_Controller
{
public function index()
{
//$this->template is inherited
//from MY_Controller
//which point to 'views/layouts/default'
//We can also load a view as data
//ie no master layout, INTO our master layout
//note we don't pass $this->template
//and set the third param as true
$dynamic_sidebar = $this->load->view('views/sidebar/dynamic', array(
'data' => 'some_data'
), true);
return $this->load->view($this->template, array(
'partial' => 'users/index' //partial view,
'dynamic_sidebar' => $dynamic_sidebar
));
}
}
Views/Layouts/default
<body>
//load the main view
//in our example we have also loaded
//a dynamic sidebar with this view
<?php $this->load->view($partial); ?>
<?php $this->load->view($dynamic_sidebar); ?>
//load a static view
//views/sidebar/static
<?php $this->load->view('sidebar/static'); ?>
</body>
Following the idea of a master page in Laravel, we can do this:
Controller code
$this->load->view('show');
View "show.php"
Set values for master page and then pass the variables to master page. Keep your master page codes inside ob_start() & ob_get_clean().
<?php $page_title = "My first page"; ?>
<?php ob_start(); ?>
Your header stylesheet links and scripts
<?php $page_header = ob_get_clean(); ?>
<?php ob_start(); ?>
<div>
<h1>This is a Header</h1>
<?php $this->load->view('Partials/list'); ?>
</div>
<?php $page_content = ob_get_clean(); ?>
<?php ob_start(); ?>
Your footer html or scripts
<?php $page_footer = ob_get_clean(); ?>
<?php $this->load->view('Layout/app',array(
'page_title' => $page_title,
'page_header' => $page_header,
'page_content' => $page_content,
'page_footer' => $page_footer
)); ?>
Partial view "Partials/list.php"
In case you don't want your code to be crowded. You can create some partial views to keep things simple.
<ul>
<li>LIst item 1</li>
<li>LIst item 2</li>
<li>LIst item 3</li>
</ul>
Master Page "Layout/app.php"
<html>
<head>
<title><?= v($page_title) ?></title>
<?= v($page_header) ?>
</head>
<body>
<?= v($page_content) ?>
<?= v($page_footer) ?>
</body>
</html>
<?php
function v(&$var)
{
return isset($var) ? $var : '';
}
So that will genarate code:
<html>
<head>
<title>My first page</title>
Your header stylesheet links and scripts
</head>
<body>
<div>
<h1>This is a Header</h1>
<ul>
<li>LIst item 1</li>
<li>LIst item 2</li>
<li>LIst item 3</li>
</ul>
</div>
Your footer html or scripts
</body>
</html>
What we probably do is separate view files for header, menu, footer, etc.. that is common for all pages. And include them inside each view. like
$this->view('header');
$this->view('menu');
//Some specific content
$this->view('footer');
If you need same functionality without copying the above to all views, you need to create a function in your controller as follows:
private function myviewfunction($current_view)
{
$this->load->view('header');
$this->load->view('menu');
$this->load->view($current_view);
$this->load->view('footer');
return NULL;
}
and call this function in all your pages (methods)
$this->myviewfunction('about'); //About is the specific view for the method

Resources