Including a sub folder in codeigniter - php

i have a problem including a folder/file in codeigniter, this is how the files are structured in my view.
-application
-model
-controller
-view
-auth
-login.php //I want to include header.php, sidebar and footer on this file
-includes
-header.php
-sidebar.php
-footer.php
on the application/view/auth/login.php i have include('../../include/header');
but is not working, i have tried several other means and no job also i cant use an absolute path because the files are in the application folder.
edit:
example in my view folder i have index.php file and i has the following codes
<?php
include('includes/header.php');
include('includes/sidebar.php');
?>
<div id='content'>
<h3>Title</h3>
<p>contents contents</p>
</div>
<?php include('/includes/footer.php'); ?>
Now in the view folder is another folder called auth and i have a file in it called login.php and has the following codes
<?php include(../includes/header.php);?> //this doesn't work
<?php include(../includes/sidebar.php);?> //this doesn't work
<div id='form'>
<form>
</form>
</div>
<?php include(../includes/header);?> //this doesn't work
on my controller i have
<?php
class Index extents CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('index'); //this works and loads all the includes
}
}
another example of a controller not working
<?php
class Login extents CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('auth/login'); //this works but doesn't load the includes
//in the login.php file found inside the auth
//folder
}
}

You can use APPPATH. APPPATH is a CodeIgniter constant. It contains the value of the full path to your application folder.
<?php include(APPPATH.'views/includes/header.php');
//your code here
include(APPPATH.'views/includes/footer.php'); ?>

Take a look a the view loader (http://ellislab.com/codeigniter/user-guide/libraries/loader.html).
Most CI functions are available with inviews, so you can use something like this:
Instead of
include('../../include/header');
use
$this->load->view('includes/header');

This is an old post, but for as many as would stumble upon it.
I'll advice that you move the include folder to your root folder. It shouldn't be in the application folder, but the folder where the application folder is.
Then you replace with
<?php include(base_url(include/header.php)); ?>
That should do the trick.

Related

Codeigniter cannot load helpers

I have checked all config files, made sure the helpers are in both system/helpers and application/helpers as well as one or the other. I have tried autoloading helpers, loading the helper in my controller and from the view and no matter what I get an error.
Unable to load the requested file: helpers/_helper.php
It is treating it as if I am trying to load a blank helper such as $this->load->helper(''); but I am using $this->load->helper('url');
This is my controller to load my home page
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Pages extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->helper('url');
}
public function index(){
$this->load->view('templates/header');
$this->load->view('home');
$this->load->view('templates/footer');
}
}
system/helpers/url_helper.php and application/helpers/url_helper.php both currently exist and have not been edited at all.
If needed I can include my autoload.php but the helper Auto-load Helper Files is currently $autoload['helper'] = array();
If you need to over ride the url helper
rename the file
application/helpers/MY_URL_helper.php
OR
application/helpers/MY_url_helper.php
Copy the whole path inside the url, like this , (wamp)
$this->load->helper('www/projectfoldername/applications/libraries/helpers/helperfile');

Css not load in Codeigniter framework

I have this code file in Codeigniter, but the URL method doesn't work also try so really I don't see problem here.
<?= link_tag('asstes/css/bootstrap.min.css') ?>
load through helper $this->load->helper('html');
What am I doing wrong?
On CodeIgniter 3 versions Just check if your base_url is set because some times having that blank can cause links not to work properly.
$config['base_url'] = 'http://localhost/your-project/';
Controller example: Welcome.php
<?php
class Welcome extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper('html');
}
public function index() {
$this->load->view('welcome_message');
}
}
Or auto load html helper
$autoload['helper'] = array('url', 'html');
In your head tag on view link_tag guide
<html>
<head>
<?php echo link_tag('assets/css/bootstrap.css');?>
</head>
Folder Example
application
assets
system
index.php
You have an error in your string path
asstes ---> assets
I think it's better to write your own helper for add your resources (js, css...)

Yii2 custom widget assets not registering

I'm trying to develop a custom widget, but I've run into a wall. I have some third-party JS/CSS files I need to include, but for whatever reason, Yii2 will not register them. I've followed everything in the Yii2 documentation but seem to get nowhere. My directory structure is as follows:
components
- DildenFeedback.php
- DildenFeedbackAsset.php
- views/
-- feedback.php
- assets/
-- feedback.min.js
-- feedback.min.css
DildenFeedback.php (Widget Class)
<?php
namespace app\components\yii2feedbackwidget;
use Yii;
use yii\base\Widget;
use app\components\yii2feedbackwidget\DildenFeedbackAsset;
class DildenFeedback extends Widget
{
public function run() {
parent::run();
DildenFeedbackAsset::register($this->view);
return $this->render('feedback');
}
}
DildenFeedbackAsset.php (Widget Asset Class)
<?php
namespace app\components\yii2feedbackwidget;
use yii\web\AssetBundle;
class DildenFeedbackAsset extends AssetBundle {
public $sourcePath = 'assets/';
public $css = ['assets/feedback.min.css'];
public $js = ['assets/feedback.min.js'];
public $depends = ['yii\web\JqueryAsset'];
}
Feedback View
?>
<script type="text/javascript">
$.feedback();
</script>
views/layouts/main.php (The main template I was trying to call in)
<?php $this->endBody() ?>
<?php
echo DildenFeedback::widget();
?>
So when I go to inspect the page, feedback.min.js/css are nowhere to be found. The view file is rendered correctly, but my guess is that my AssetBundle is improperly formed. Any help would be much appreciated.
EDIT (From the Yii Debugger Assets):
sourcePath /var/www/public/components/yii2feedbackwidget/assets
basePath /var/www/public/web/assets/7e80049a
baseUrl /assets/7e80049a
css assets/feedback.min.css
js assets/feedback.min.js
depends yii\web\JqueryAsset
EDIT 2
I have tested this on a fresh install of Yii2, and I get the same error.
Well, you should simply correct your sourcePath :
class DildenFeedbackAsset extends AssetBundle {
public $sourcePath = '#app/components/assets/';
public $css = ['feedback.min.css'];
public $js = ['feedback.min.js'];
public $depends = ['yii\web\JqueryAsset'];
}
You should also fix your view by using registerJs() instead of a script tag :
$this->registerJs('jQuery.feedback();');
I've solved it, and I feel a bit silly. What it came down to, was the location of the widget call. As I noted in my original question, I was echoing the widget like so:
<?php $this->endBody() ?>
<?php
echo DildenFeedback::widget();
?>
However, I did not pay attention to where that call was. It NEEDS to be before the endBody call
<?php
echo DildenFeedback::widget();
?>
<?php $this->endBody() ?>
This ended up solving the issue. Thanks to Soju for directing me towards the widget call.

Putting additional pages on kohana framework

I've started reading kohana documentation but didn't really understand, the index page I need to define in controllers like public $template = "index"; but how do I add other html, php files as links? Because it doesn't find them if I simply put them in the view's folder.
if you need to create a new page first you need to create a function in controller. also you need to assign the view file in that function.
For ex,
Here i ll create a user login page in user controller.
<?php
Class Controller_User extends Controller_Welcome
{
/**For get User Login page**/
public function action_login()
{
$view= View::factory('login');
echo $view;
}
}
?>
login.php file is placed in application/views/login.php
Now your login page is called in the url like http://mysite.com/user/login/
If you want to call the page in a common template file , first you need to assign template file. then you can easily call the page in template file.
For ex,
user.php controller:
<?php
Class Controller_User extends Controller_Welcome
{
/**For get User Login page**/
public function action_login()
{
$this->template='template.php';
$view= View::factory('login');
$this->template->content = $view;
}
}
?>
template.php view file:
-- your html datas here --
<?php
echo new View("header");
?>
-- your html datas here --
<?php
echo $content; ?>
-- your html datas here --
<?php
echo new View("footer");
?>
-- your html datas here --
Here header is header.php, footer is footer.php. all these files are placed in applications/views/ folder.
public $template = "index" means that Controller_Template class will load view from views/index.php file. You can add requred links directly to this file or dynamically - with template variables or subtemplates.
This wiki may help: http://kerkness.ca/kowiki/doku.php

CodeIgniter template library problems

I am trying to use a template library with my current codeigniter project. However I am getting the following error message,
Unable to load the requested file: main.php
My file system looks like this,
/application
/themes
/views
/layouts
admin.php
In MY_Controller.php I have the following,
<?php
class Dashboard extends MY_Controller {
public function __construct()
{
parent::__construct();
}
}
?>
In my controller I have the following
<?php
class Dashboard extends MY_Controller {
public function __construct()
{
parent::__construct();
//$this->template->set_layout('admin');
}
public function index()
{
$this->template
->set_layout('admin') // application/views/layouts/two_col.php
->build('welcome_message'); // views/welcome_message
}
}
It depends what template system your using, from the sounds of it you're using Phil Sturgeon's template library.
If so open up config/template.php
and find this line:
$config['layout'] = 'main';
And edit it as applicable.
Which template system are you using? I guess it needs a file named main.php which will load the view. Or the file does not exist and you will have to add it or the path to the file is not correct

Categories