breadcrumbs in codeigniter (MVC) - codeigniter

I want some help with breadcrumb in codeigniter. It's my first experience with CodeIgniter and I don't have to clear enough how can I declare this function at Controller
public function breadcrumb(){
$this->load->library('breadcrumb');
function Tutorial() {
parent::Controller();
$this->load->library('breadcrumbcomponent');
$this->breadcrumbcomponent->add('Home', base_url());
$this->breadcrumbcomponent->add('Tutorials', base_url().'tutorials');
$this->breadcrumbcomponent->add('Spring Tutorial', base_url().'tutorials/spring-tutorials');
}
}
in View. I create the library as it is showing here:
http://www.technicalkeeda.com/codeigniter-tutorials/how-to-create-bread-crumb-using-php-codeigniter
Help me to understand better and to solve this case!
Best :)

Please edit your question and include proper code tags where needed. I will assume that you are using new version of CodeIgniter, but old tutorial (one that is related to CI version less than 2). Try to change this in code should be working that way.
In controller, change this code:
function Tutorial(){
parent::Controller();
$this->load->library('breadcrumbcomponent');
}
with this one:
public function __construct()
{
parent::__construct();
$this->load->library('breadcrumbcomponent');
}
Library class could stay the same, I'd say.

Related

how can i create shortcodes for include a page in blade

I want to create shortcodes system for my Laravel project to include a page in another page.
Anyone have a solution?
[include]test[/include] -> #include('test');
This package is probably what you're wanting.
Something like:
class IncludeShortcode {
public function register($shortcode, $content, $compiler, $name)
{
return view($content);
}
}
Shortcode::register('include', 'IncludeShortcode');
Not tested, but should get you going in the right direction.

How can I add an external code or script to Codeigniter as class library?

I have an external script or code, for example, including functions, classes, and database inside it. Is there any way to add it to Codeigniter as class library? If yes, please explain more details as possible. My script has some important files included a number of good functions and classes, even database connection and mysql queries inside, but I am confused about how to add it to Codeigniter without rewriting code lines in Codeigniter. Suggest me some good methods to resolve my problem effectively as possible because I do not want to spend time on rewriting and I am a new beginner of Codeigniter. Thank you very much for your help!
The user guide is pretty well explained
http://www.codeigniter.com/user_guide/general/libraries.html
http://www.codeigniter.com/user_guide/general/creating_libraries.html
go to application/library put your class into
Library file name lib.php
class lib {
public function __construct()
{
}
public function test(){
return "welcome";
}
}
go to application->controllers
class Hall_list extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->library("lib");
echo $this->lib->test();
}
}
In this way you can load library to your controller

codeigniter method structure, can someone explain me?

Im really curious how does codeigniter achieve something like this:
$this->upload->do_upload($field_name)
it looks like method chaining, but it's not. How would the structure of this look in plain OOP?
I suppose its not as simple as..?
public function upload()
{
// stuff
return $this;
}
public function do_upload()
{
// stuff
return $foo;
}
Cheers!
When you load the library in your controller it's actually doing something like this behind the scene.
include 'system/libraries/Upload.php';
$this->upload = new CI_Upload();
Now you have "$this->upload" ready to use,
Next when you call "$this->upload->do_upload()" you're actually calling a method within the library.
On the other hand Method chaining, is just a matter of making methods returns an instance of the same object, You can review this in libraries code in CodeIgniter 3 on GitHub.
Where most library uses method chaining now.

How to make urls like category/5/sub_category in CodeIgniter

I want my urls to look like:
www.domain.com/catalog/category_name/category_id/product_name/product_id.
How should my controllers look like to accomplish this?
It's ok for the Controller to have Catalog and Function category_name in it.
But what will be my product controller and function. How can I make a structure like this.
Do I need to have a specific file structure?
I use CodeIgniter framework. Thanks for your help.
In general the controller must not be changed (if it accepts the product id as parameter).
All other information can then be put in the url from the database (querying via the product id and getting related information) through URI Routing changes.
your controller method should be like this
<?
//..
public function category_name($category_id,$product_name,$product_id){
// some cool code
}
//..
?>
As you question is very vague - so is my answer - but this function achieves what you asked
Class Catalog extends CI_Controller
{
public function category_name ($category_id, $product_name, $product_id)
{
// your function
}
}

Codeigniter common templates

Let's say that I have a website that has 100 different pages. Each page uses a common header and footer. Inside the header is some dynamic content that comes from a database.
I'd like to avoid having to have code in every single controller and action that passes this common code into the view.
function index()
{
// It sucks to have to include this on every controller action.
data['title'] = "This is the index page";
data['currentUserName'] = "John Smith";
$this->load->view("main_view", data);
}
function comments()
{
// It sucks to have to include this on every controller action.
data['title'] = "Comment list";
data['currentUserName'] = "John Smith";
$this->load->view("comment_view", data);
}
I realize that I could refactor the code so that the common parts are in a single function and the function is called by the action. Doing so would reduce SOME of the pain, but it still doesn't feel right since I'd still have to make a call to that function every time.
What's the correct way to handle this?
One way I have been doing this is to extend the default controller class. You can read up on extending classes with MY_Controller in the user guide. Inside this extended class you can include something that you ALWAYS want to do, like render the page header template before the main content, or authorise a users access etc.
class MY_Controller extends Controller {
function __construct()
{
parent::Controller();
//code to always do goes here
echo 'Always print this comment';
$this->load->view('partials/template_start');
}
}
Then you can have your normal controller class extend THIS class by using
class MyControllerNameHere extends MY_Controller {
function __construct()
{
//setup here
}
function index()
{
echo 'Only print this bit when this method is called';
$this->load->view('partials/MYPAGENAMEHERE');
}
}
There are other ways of doing this, I use a mixture of the above and William's Concepts Codeigniter Template library. Do a bit of searching - there are a few solutions for you.
I had a similar situation. I created an 'includes' folder, and in there put a file that had the repetitive code from my controllers. Then in the controllers just include('/path/to/includeFile.php');
Don't know if it's the "correct" way, but it works well for me.
I ran across this after a search of their site. http://codeigniter.com/wiki/Header_and_footer_and_menu_on_every_page/ I'll review this page and its links, then post my thoughts.

Resources