To link in CodeIgniter within application - codeigniter

<a class="navbar-brand" href="<?php echo base_url ('welcome/aboutus');?> "> About Us </a>
Here welcome is controller name and aboutus is page name. I have set autoload. If I run this then it shows "Object not Found", what's wrong with that?

Your question is a bit hard to help you with because you have limited code to go by.
You may need to set your base url
$config['base_url'] = 'http://localhost/yourproject/';
$config['index_page'] = '';
Welcome is the controller name as you said and aboutus would be a function name
Follow the PHP Codeigniter Naming Style Guide
Welcome.php
<?php
class Welcome extends CI_Controller {
public function index() {
}
public function aboutus() {
}
}
You may need a suitable .htaccess file to remove it so can work with out index.php
application
system
.htaccess
index.php
Try with <?php echo base_url ('index.php/welcome/aboutus');?>

In CodeIgniter, it dose not work like that.
<?php base_url ('welcome/aboutus') ?>
What it means is that Welcome is the name of the controller and aboutus is the name of the function in the controller.
So when you click on the link generated by the above code, it will search for a function named aboutus in the controller file Welcome.
To load about us using your link, the controller should look like this,
<?php
class Welcome extends CI_Controller {
public function index() {}
public function aboutus() {
$this->load->view('Name of the view file');
}
} ?>
I think you should know What is MVC Framework? and How Dose it Work?.
Have a look at these::
CI MVC Framework
CI MVC Static Page

Related

What's good way to load header and footer view in codeigniter?

I'm using codeigniter 3x. I'm working on my website. I'm using include method in my view.
Like
<?php include('templates/header.php'); ?>
<h1>Home Page</h1>
<?php include('templates/footer.php'); ?>
Is this a good way to show header and footer in codeigniter.
Thank!
You are half your way, here is how you will be able to make it more dynamic, in your views file you should have a structure like this:
views
- header.php
- footer.php
- template.php
- home.page
In header.php you should have all your header and footer content which you wants to display on all pages.
Now in your template move all your includes.
template.php
<?php $this->load->view("header.php"); ?>
<?php $this->load->view($main_content); ?>
<?php $this->load->view("footer.php"); ?>
here you notice $main_content variable, it is dynamic file name which we want to load in our controller. So lets assume you have a controller like this:
public function home()
{
$data['meta_title'] = $this->lang->line('home_meta_title');
$data['meta_description'] = $this->lang->line('home_meta_description');
$data['meta_keywords'] = $this->lang->line('home_meta_keywords');
$data['main_content'] = 'home';
$this->load->view('template',$data);
}
$data['main_content'] = 'home'; is loading your home.php file, you can also load from subdirectories like 'directory/home'. You can also pass any variable like I gave you above example with dynamic meta.

Use master template in codeigniter

I have two controllers, two views and a masterpage:
dashboard controller:
class Dashboard extends CI_Controller {
public function index()
{
if($this->session->userdata('login') == true){
$data['title'] = 'Dashboard';
$data['content'] = 'pages/dashboard';
$this->load->view('layout/master', $data);
}
else{
redirect('auth');
}
}
customers controller:
class Customers extends CI_Controller {
public function index()
{
$data['content'] = 'pages/add_customer';
$this->load->view('layout/master', $data);
}
public function add(){
$data['content'] = 'pages/add_customer';
$this->load->view('layout/master', $data);
}
}
And my master page:
<head>
<meta charset="utf-8" />
<title><?php echo isset($title)? $title: NULL; ?></title>
<?php $this->load->view('layout/header'); ?>
</head>
<body>
<!-- BEGIN PAGE BASE CONTENT -->
<?php $this->load->view($content);?>
<!-- END PAGE BASE CONTENT -->
</body>
The problem is When i call dashboard, the view runs in the template and everything is fine. When i call customers, again everything runs fine and the template is OK. But when i call the add method from customers controller, It's like the master page doesn't work and the template messed up. Like there is no CSS or something.
What's the problem?
Thanks in advance :)
I personally do not rely on native CI functionality for any template/view stuff. I use Stencil, which isn't actively developed anymore, but it worked on 2 and works on 3. I modified the core library file to handle session and config variables, but this thing is beautiful. It's at the center of every one of my CI deployments.
Stencil on scotch.io via Github
This absolutely doesn't answer your question and certainly deserves to be down-voted or flagged for that reason. But CI view handling is awkward, so I never endorse using it. I don't know why Stencil isn't built in.

Site URL CodeIgniter Not Working

I have Controller on CI like this
class Testing extends CI_Controller {
//put your code here
public function xx() {
$this->load->helper('url');
$this->load->view('testing');
}
public function linkURL() {
$this->load->helper('url');
$data['test'] = "testing123";
$this->load->view('xxx_view', $data);
}
}
I'm running on function linkURL and call view xxx_view, the code on view like this
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
$segment = array('Testing', 'xx');
?>
Link
</body>
view call a href and using helper site_url to call Controller Testing and function xx. But the link is not working. I am already capture on firebug and link looks like weird. The Link on href contain *http://::1*. How to solved that link
You can print_r($_SERVER) in your controller and check it. or You can use
$config['base_url'] = 'http://localhost:8081/your-project/'
In my opinion, seem to you sent data test to view xxx_view but not using this variable. try echo $test and I see in url using xxx not xx
You need to autoload URL helper in your config/autoload.php.This time you are loading URl helper in function. That will not work for your view.

Laravel 5 How to dynamically include External CSS and JS file depends on controllers/routes

I'm wondering what could be a good way to
dynamically include a js file in my main blade
template like this
<?php
$currentRoute = Route::currentRouteName();
$currentRoute = $currentRoute?$currentRoute:'home';
$currentRoute = explode('.',$currentRoute);
?>
<script src="{{$currentRoute[0]}}.js"></script>
but just a less tricky :)
I did something like that in that past.
Here, is my "trick" to do this.
First you need a top level controller class
class MainController extends Controller {
protected static $js_files = [];
protected static $css_files = [];
public static function boot()
{
view()->share('js_files', static::$js_files);
view()->share('css_files', static::$css_files);
  parent::boot();
}
}
Then, all of your controllers must inherit from your MainController class.
The reason I use the keyword "static::" instead of "self::" is because I want to retrieve the latest child properties and not the "MainController" property, which would gave me the keywords "self::".
See http://php.net/manual/en/language.oop5.late-static-bindings.php
In your blade layout, in the head section do the following :
#foreach($css_files as $src)
<link href="{{$src}}" rel="stylesheet">
#endforeach
and before your closing body tag
#foreach($js_files as $src)
<script src="{{$src}}"></script>
#endforeach
In each controllers, you have to put in the $js_files and $css_files inherited properties the sources of all of your js/css.
if you want to include a JS/CSS only for one route, you can add the source of the file directly in the method.
And voilà !

CodeIgniter - Including sections of code?

I have about 9 lines of code that will be the same in many of my controllers. They go in the index and are used to check whether the user is logged in, what level of access has been granted, and grab some session variables. What's the best way to include snippets of code that are not complete functions within themselves?
The snippets are not in all controllers, just the ones that build the admin section of the application.
It would be great if I could use one line of code to include the snippet but I'm new to CodeIgniter and I don't want to stray from best practices. If you could include a brief example that would help me visualize this. Thanks!
You want to create an extension of the base CI controller and do your checks there. Extending the cores are in the documentation here: http://ellislab.com/codeigniter/user_guide/general/creating_libraries.html Scroll down to the Extending Native Libraries part.
In your new controller you can do something like this:
<?PHP
class MY_Controller extends CI_Controller
{
function __construct()
{
parent::__construct(); //Gets all the CI_Controller functions and makes them available to this controller.
if($this->session->userdata('is_logged_in')
{
$this->load->model('categories');
$this->categories=$this->categories->getAllCategories();
}
}
}
Then in your other controllers you use MY_Controller instead of CI_Controller like this:
<?php
class Pages extends MY_Controller {
Obviously you'll have your own checks and variables to load but this should give you the general idea. In the example above I can now use $this->categories to retrieve my categories anywhere in the application that's been loaded with MY_Controller.
I'm using a 'main' template file (views/template.php):
// views/template.php
<?php $this->load->view('includes/header'); ?>
<?php $this->load->view('includes/' . $main_content); ?>
<?php $this->load->view('includes/sidebar'); ?>
<?php $this->load->view('includes/footer'); ?>
So the view directory structure is something like this:
--views
--includes
- header.php
- footer.php
- template.php
As you can see inside the view/includes/ folder I'm using some reuseable snippets like 'header.php', 'footer.php'...
So inside a controller I'm loading this main template file:
class MyController extends CI_Controller {
public function index()
{
//...
$this->load->view('template',$data);
}
}
So it loads 'template.php'.
And can use the $data inside header.php, footer.php too..
But you can vary this as you want, it's just a 'basic' idea..

Resources