Codeigniter 3 - how to callback to home - php

if i´m on a category/index/$1 Site of my Page and want to call back to domain.com/home, it doesn't work for me.
Thats my routes
$route['default_controller'] = 'home';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['admin'] = 'admin/login';
$route['category/(:any)'] = 'category/index/$1';
$route['category/(:any)/(:num)'] = 'category/index/$1/$2';
$route['page/(:any)'] = 'page/index/$1';
If i call domain.com/about and click on my navigation "home", its working. but if i make domain.com/aboutdetails/index/1 he make on home click this one:
domain.com/aboutdetails/index/home
htaccess
#RewriteEngine on
#RewriteCond $1 !^(index\.php|images|robots\.txt|style|cdn|check\.php)
RewriteEngine on
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

I think, you can use :
Home

If you want go to home with your configuration router using domain.com.
If you want use domain.com/home you add next line route:
$route['home'] = 'home/index';
Considerer use initial en and limiter( regex ), example:
$route['^home$'] = 'home/index';
Good, To print link don't use base_url(), it is use to load assets or other an return project root, Use site_url().
In the application/config.php set empty to $config['index_page']:
//$config['index_page'] = 'index.php';
$config['index_page'] = '';
And then create link with site_url():
site_url( 'home' );
If you want create link with more params you maybe pass array.
site_url( array( 'home', $param2 ) );

Related

How can I remove method name from URL in codeigniter?

I am using the multilingual feature in my Codeigniter website.
current URL looks like
http://example.com/hi/news/view/news-title
I want this like
http://example.com/hi/news/news-title
current routes.php is looking like
$route['default_controller'] = 'home';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
//Routes for multilingual url
$route['^hi/(.+)$'] = "$1";
$route['^en/(.+)$'] = "$1";
// '/en' -> use default controller
$route['^hi$'] = $route['default_controller'];
$route['^en$'] = $route['default_controller'];
.htaccess file looks like
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|(.*)\.swf|forums|images|css|downloads|jquery|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?$1 [L,QSA]
I tried these but it not works and always give me 404 error
$route['^(en|hi)/news/(:any)'] = "news/view/$1";
$route['hi/news/(:any)'] = 'news/view/$1';
You have not specified the name of the controller or the controller called "News" is not exists.
Specify the name of the controller that is having the "news" method e.g.
$route['(en|hi)/news/(:any)'] = "home/news/view/$1";
OR
Create a "News" controller and create a "view" method e.g.
class News extends CI_Controller {
public function view()
{
}
}

CodeIgniter base_url leads to page not found

I'm using CodeIgniter, and I'm new at it, as I'm in web development.
I'm having a problem with the base_url from CI: it leads to 404 Page not found.
Here is the referrence for the page:
<li>Cadastrar</li>
The controller's function:
function novo_usual(){
$this->load->view('html-header');
$this->load->view('html-footer');
$this->load->view('cadastro_usual');
}
My controller's name file starts in capital, like Usual.php, and it extends CI_Controller.
My altoloado.php:
$autoload['helper'] = array('url','html','form','funcoes');
My routes.php:
$route['default_controller'] = 'Home';
Finally the .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L]
</IfModule>
See \application\config\config.php
$config['base_url'] = '';
write to this your site url.
for example
$config['base_url'] = 'http://test.com/';
and now if you use base_url('Usual/novo_usual') this is equal http://test.com/Usual/novo_usual
If you don't set site url then first of set its
//Site URL Path : application\config\config.php
$config['base_url'] = 'http://localhost/site_name';
Then add this :
<?php
echo site_url('Usual/novo_usual');
//OR
echo base_url('Usual/novo_usual'); ?>
At first you need to set your folder path in
\application\config\config.php
$config['base_url'] = '';
for example
$config['base_url'] = 'http://localhost/folder/';
I've managed to solve it.
Turns out that my project's name starts with 'G', and in my url I was calling it with 'g'.
Thanks guys.

Default Controller of codeigniter shows blank page

here is my url structure-
http://example.com/demo
demo directory contain my all codeigniter application files. I set my base path like this
$root = "http://".$_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url'] = "$root";
and also set $config['index_page'] = ''; is blank. My .htaccess file contain-
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?$1 [QSA,L]
Suppose i have a controller called Home and contain one method called index. I have set it as my default controller like this on my routes.php
$route['default_controller'] ='home';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
// Catch all route
$route['home'] = 'home/index';
Now the problem is when i am using in my localhost(wamp) it works well. But when I uploaded it to my server and go to this url http://example.com/demo it shows blank page. It should show home controller. I have checked it that if problem present in Home controller. Thats why I go to this url http://example.com/demo/home now its working fine but problem occurred when using the base url.
It's not needed to enter the route for your default controller.
Just remove the line with $route['home'] = 'home/index';; I think it's overriding your default configuration.

Set route in different directories Codeigniter

Divided controllers into folders, site in site folder, admin in admin folder (inside controllers).
But I can not access the controllers without putting the base of the same folder url, example:
I want to access like this: http://localhost/gabriel/projeto/about
But just so I can: http://localhost/gabriel/projeto/site/about
File 'htaccess' this so:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
'base_url' this so:
$config['base_url'] = 'http://localhost/gabriel/projeto/';
File 'routes' this so:
$route['default_controller'] = "site/index/";
$route['404_override'] = '';
$route['admin'] = "admin/login";
Already tried modifying the .htaccess but no result.
Inserting another line in the file 'routes':
$route['(:any)'] = "site/$1";
But putting that line it conflicts with the route ends and the Admin can receive only one parameter in Url.
How can I solve this problem? make dynamic route...
I'm not 100% sure this is what you're asking, but try setting the base_url in the config/config.php file
$config['base_url'] = 'http://localhost/gabriel/projeto/';
or set to whatever you want your site root to be
add this in your routes.php.. place it above all other $route variable...
$route['projeto/about'] = 'projeto/site/about';
Solved my problem as well:
$route['admin/(.*)'] = "admin/$1";
$route['(.*)'] = "site/$1";
Now this dynamic route

Hiding Default Controller Name in Codeigniter

When my website opens for the first time , it directly goes to this address: http://www.website.com/tr/main
But I want my link to be like this: http://www.website.com/
I can't remove "tr/main" links. How can I solve this problem?
Php Code:
$route['default_controller'] = "main";
$route['404_override'] = '';
$route['(tr|en)/(.+)$'] = '$2';
$route['(tr|en)$'] = $route['default_controller'];
Htaccess Code:
RewriteEngine On
# Put your installation directory here:
# If your URL is www.example.com/, use /
# If your URL is www.example.com/site_folder/, use /site_folder/
RewriteBase /
# Do not enable rewriting for files or directories that exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# For reuests that are not actual files or directories,
# Rewrite to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]
In your config.php file write this code :-
$config['base_url']='http://Your Path/';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
$config['subclass_prefix'] = 'Base';
In your routes.php file write this code :-
$route['default_controller'] = "home";
$route['404_override'] = '';
Write the follwing code in your routes.php file :-
$route['default_controller'] = "main";
$route['404_override'] = '';
and then in your "main" controller i.e in main.php use
function index()
{
// write all your code inside this function
}

Categories