get uri segments from default controller codeigniter - php

i have codeigniter setup to use "home" as the default controller which is to display homepage
i tried to run the index function within home controller to collect the uri segments to determine whether or not to redirect or display home page but it seems to think that the uri segment is another controller mysite.com/segment1
function index(){
$url1 = $this->uri->segment(1);
if(!empty($url)){
echo $url1;
}
else{
$this->load->view('home_page');
}
}

You are setting $url1 as the segment then testing if $url is empty or not.
From your code, $url will always be empty.
Make sure you keep an eye on what you call your variables.
So what you have now...
function index(){
$url1 = $this->uri->segment(1);
if(!empty($url)){ // $url will always be empty as it's not defined.
echo $url1;
}
else{
$this->load->view('home_page');
}
}
What you should have...
function index(){
$url = $this->uri->segment(1);
if(!empty($url)){
echo $url;
}
else{
$this->load->view('home_page');
}
}
Then it will do as you intended.

You can set default controller to home. Then to be able to read $this->uri->segment(3); you nead to pass thet uri via URL mysite.com/controller/function/uri-segment(3). If you hav an Index function in home controller mysite.com and you want to pass a value of 11 via uri your url shuld look like this:
mysite.com/home/index/11

For this to work i had to change the routes to
$routes[('any')] = "New_controller"; //created a new controller
in "New_controller" i could then define the first segment i want to get
function index(){
// define segments
$url = $this->uri->segment(1);
if(!empty($url)){
echo $url;
}
and of course my
$routes['default_controller'] = "home";
function index(){
// home controller
$this->load->view('home_page');
}
Now when i go to mysite.com it takes me to the homepage or if i go to mysite.com/12345 it takes me to the new controller page where i can run a query on the segment

Related

Passing value through url

I am trying to send a url from view page to controller but it does not seem to work the way i am thinking.
View Page
Product
User
I want to get "tbl_product"
Controller admin
<?php
class Admin extends CI_Controller {
public function test() {
echo $this->uri->segment(4);
}
}
?>
but if the segment(4) is changed to segment(3), it shows up with displaying "product" in the screen
your controller function should have arguments for your url segments
for example:
public function test($product = 'product', $tbl = 'tbl_product') {
echo $tbl // contains the string tbl_product
}
Since you said your routes look like this:
$route['default_controller'] = "admin";
$route['404_override'] = ''
and your URL is like this:
<?= base_url() ?>admin/test/product/tbl_product
Then if your base_url() is localhost/my_app, your URL will be read as this:
http://localhost/my_app/admin/test/product/tbl_product
http://localhost/my_app/CONTROLLER/METHOD/PARAMETER/PARAMETER
So in your controller, you can do this:
class Admin extends CI_Controller {
public function test($product = NULL, $tbl_product = NULL) {
echo $product;
echo $tbl_product;
}
}
It's strange to use codeigniter for this purpose, because codeigniter uses as default the url format bellow.
"[base_url]/[controller]/[method]"
I think it will be better and more easy to just pass the values you want as get parameters and make some httaccess rules to make your url more readable for the user and robots. That said you can do that:
Product
<?php
class Admin extends CI_Controller {
public function test() {
echo $this->input->get('product');
//should output 'tbl_product'
}
}
?>
If you prefer to use uri instead u should route your uri's so it will be like.
In your routes file you probably I'll need something like this.
$route['product/(:any)'] = "Admin/test";
This way you will probably access the uri segments correctly.
Thank you so much for going through.
$this->uri->segment(4); // is now working :S
its not working properly after all I made changes to routes.php and came back to default again. I seriously have no idea what the reason behind not displaying the result before.

How to use redirect in codeigniter controller class?

Can any one tel me how to use redirect in controller class.
I am wrote below code:
Controller:-
<?php
class Login extends CI_Controller {
public function result()
{
$name = $this->input->post('name');
$email = $this->input->post('email');
$this->index();
redirect('/success', 'location');
}
}
view:-
success.php
<?php
echo "Success page";
?>
It shows error message 404 Page Not Found.
I have load all required helper classes in autoload class.
All you need to do to redirect is
Example:
public function index() {
redirect('controller_name');
// you may need to set controller name in routes do not need location
redirect('folder/controller_name');
// you may need to set controller name in routes do not need location
}
In Codeigniter , redirect method takes 3 parameters.
redirect('/controller_name/method_name', 'location', 301);
First parameter is the uri path which you want to redirect. The second parameter is optional and takes "location" method (default) or the "refresh" method. The third optional parameter is status code. You can check detail on documentation.
Edit
function success () {
$data["message"] = "Success";
$this->load->view("success", $data);
}
views/success.php
<?php echo $message; ?>
You have to pass data in array because codeigniter use extract method to pass value in view so that you can use arrary key as variable.
Hope it will be useful for you.
You might be having these three files
routes.php where you can set your routes as like
$route['success'] = 'your_controller_name/your_method_name';
E.g.
$route['success'] = 'my_controller/success';
then within your_controller.php, there you have a method as
function success() {
$data['msg'] = "Success";
$this->load->view('success',$data);
}
and within your success.php
<?php echo "<h3>".$msg."<h3>";?>

Codeigniter routes for passing get parameter to index function

I have a url www.mywebsite.com/store/123456
where store is my controller class and I have a index function in it where Im getting the value after after store ,ie 123456.But im not able to achieve it.
As found online,I have tried adding this to routes $route['store/(:any)'] = 'store/index'; also tried
$route['store/(:any)'] = 'store/index/$1';
but doesn't seem to work.Please help me out.
In controller index function is
public function index()
{
echo $this->uri->segment(1);
}
But its not working.Pleae help
You are invoking 123456() method instead of index() method therefore you get CI's 404.
The simplest way is to use this kind of route
$route['store/(:any)'] = 'store/index/$1';
AND in top of it add parameter to index function in your case
public function index($parameter)
{
echo $this->uri->segment(2);
}
note that I changed segment parameter please see documentation.
using _remap()
function _remap($parameter){
$this->index($parameter);
}
function index($p)
{
echo $p; //shows 123456
}
If I remember correctly:
segment(n) gives you the segments of your uri before the routing takes place
rsegment(n) gives you the segments of your uri after routing (if routing occurred or the same as segment if it didn't)
so for /store/123456 getting rerouted to /store/index/123456
segment(1) == rsegment(1) == 'store'
rsegment(2) == 'index'
segment(2) == rsegment(3) == '123456'
Route:
$route['terms_and_conditions'] = 'SO_front/page/1';
controller :
public function page($id)
{
echo $id;
}
Output :
1
Here my solution for CI3...i would prefer laravel for advanced routing, orm,restapi, build in vuejs.
$route['store/(:any)'] = 'store/index/';
public function your index(){
$parameter=$this->uri->segment(2);
//use parameter for sql query.
}
Let say your route $route[store/product/update/(:any)] , then $parameter=$this->uri->segment(4)
Problem?. You will need to change entire file code if you plan to change the route name include view, controller, and custom route.

Codeigniter Setting Homepage ( Default Controller )

I'm trying to implement page templating in my codeigniter application, templating is working fine for instance, i have a blog page, which i'm trying to assign as my homepage, with different view and pagination and so on. When i write www.mywebsite.com/blog, it gets opened in homepage template, or assume that i have a page www.mywebsite.com/about , it gets opened in page template too. But when i try to access my website via www.mywebsite.com, i have a 404 error page. How can i assign my blog page as the homepage ?
Page Controller :
class Page extends Frontend_Controller {
public function __construct(){
parent::__construct();
$this->load->model('page_m');
}
public function index() {
// Fetch the page template
$this->data['page'] = $this->page_m->get_by(array('slug' => (string) $this->uri->segment(1)), TRUE);
count($this->data['page']) || show_404(current_url());
add_meta_title($this->data['page']->title);
add_meta_keyword($this->data['page']->keywords);
add_meta_description($this->data['page']->description);
// Fetch the page data
$method = '_' . $this->data['page']->template;
if (method_exists($this, $method)) {
$this->$method();
}
else {
log_message('error', 'Could not load template ' . $method .' in file ' . __FILE__ . ' at line ' . __LINE__);
}
// Load the view
$this->data['subview'] = $this->data['page']->template;
$this->load->view('_main_layout', $this->data);
}
private function _page(){ // methods }
private function _homepage(){ // methods }
}
in my Routes, i have set my default controller to page controller
$route['default_controller'] = "page";
application/config/routes.php
$route['default_controller'] = 'namecontroller';
The problem is that there's no URI segment when you visit www.mywebsite.com. You can try setting the default value as:
$this->uri->segment(1 , 'blog')
Your code
// Fetch the page template
$this->data['page'] = $this->page_m->get_by(array('slug' => (string) $this->uri->segment(1)), TRUE);
count($this->data['page']) || show_404(current_url());
the uri segment code
$this->uri->segment(1)
is looking first url segment, when you browse your site like www.yousite.come/blog it will work find but when you do www.yoursite.com 1st uri segment is missing so it will return false , so i will show 404 page.
Solution :
You can just add the second parameter to the function like
$this->uri->segment(1,'blog');
now if the first url segment is missing it will not return false,it will return the default vlaue 'blog'
For more information about this you can see codeingitor documentation

SEO url gives 404-error in CodeIgniter

I am pretty new to codeigniter. I do know php.
How can I accomplish to load the right view?
My url: /blog/this-is-my-title
I’ve told the controller something like
if end($this->uri->segment_array()) does exist in DB then load this data into some view.
I am getting an 404-error everytime I access /blog/whatever
What am i seeing wrong?
unless you're using routing, the url /blog/this-is-my-title will always 404 because CI is looking for a method called this-is-my-title, which of course doesn't exist.
A quick fix is to put your post display code in to another function and edit the URLs to access posts from say: /blog/view/the-post-title
A route like:
$route['blog/(:any)'] = "blog/view/$1";
may also achieve what you want, if you want the URI to stay as just `/blog/this-is-my-title'
The may be more possibilities:
The most common - mod_rewrite is not active
.htaccess is not configured correctly (if u didn't edited it try /blog/index.php/whatever)
The controller does not exist or is placed in the wrong folder
Suggestion: if you only need to change data use another view in the same controller
if (something)
{
$this->load->view('whatever');
}
else
{
$this->load->view('somethingelse');
}
If none of those works post a sample of code and configuration of .htaccess and I'll take a look.
The best way to solve this problem is to remap the controller. That way, you can still use the same controller to do other things too.
No routing required!
enter code here
<?php
class Blog extends Controller {
function __construct()
{
parent::__construct();
}
public function _remap($method, $params = array())
{
if (method_exists($this, $method))
{
$this->$method();
}
else
{
$this->show_post();
}
}
function index()
{
// show blog front page
echo 'blog';
}
function edit()
{
// edit blog entry
}
function category()
{
// list entries for this category
}
function show_post()
{
$url_title = $this->uri->segment(2);
// get the post by the url_title
if(NO RESULTS)
{
show_404();
}
else
{
// show post
}
}
}
?>

Categories