Im not to good in php and codeigniter gives me maximum. My site is a search engine and after i do a search, url of my site is like this: mysite.com/?search=eminem&type=mp3 and i don't like it.
I want to do it so when i type search eminem to give me this url: mysite.com/eminem/mp3
Here is code from CI
<?php
class Pages extends CI_Controller {
public function view($page = 'home')
{
if ( ! file_exists('application/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
what other code do i have to add here to transform the qwery strings to paths?
here is routes.php
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';
$route['404_override'] = '';
Here is my search form from home.php
<form id="searchwrapper" name="searchform"> <input type="text" class="searchbox" id="txtSearch" name="search" /> <input type="image" src="../images/empty.png" class="searchbox_submit"/>
<p id="circ">
<input type="radio" id="mp3" name="type" checked value="mp3"/>
<label class="circ-btn normal" for="mp3">Mp3</label></p>
<p id="circ">
<input type="radio" id="album" name="type" <?php echo $radio2;?> value="album"/>
<label class="circ-btn normal" for="album">Album</label></p>
</form>
here is .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /ci/
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
Assuming mod_rewrite is already working, then you can set this in your routes.php
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';
$route['(:any)/(:any)'] = "pages/search/$1/$2";
I'm differentiating pages/search from pages/view so it would be less confusing.
By the way, this has to be the last route in your routes.php file so it has low priority and won't conflict with specific routes like the samples below. Because if it's on top of your routes file, then accessing users/list will be treated as search=users&type=list.
$route['users/list'] = "users/list";
$route['pages/list'] = "pages/list";
Then in your pages controller.
public function search($search = '', $type = '')
{
// $search now contains $1 and $type contains $2
// instead of $_GET['search'] and $_GET['type']
}
Hope that helps.
Related
I try to redirect to a specific function in the same controller, but it doesn't redirect and only shows a white page (blank).
Trying to redirect to some specific url, it's still not moving to the url.
signin controller:
public function index()
{
$this->generateToken();
$data = array(
'title' => 'MASUK',
'breadcrumb' => 'MASUK',
'menu' => 'MASUK',
'login' => 'active',
'captcha' => $this->refresh_captcha(),
'msg' => '',
'infoMSG' => ''
);
if (isset($this->session->userdata['logged_in']['usr_email']) == "") {
$this->template->load('template', 'signin', $data);
} else {
//var_dump(base_url()); // show the base url
//redirect('https://www.facebook.com'); // not direct to specific url too
redirect('Signin/logout');
}
// else {
// redirect('landing');
// }
}
public function logout()
{
print_r("mask"); //not print anything
$this->load->library('session');
$this->session->unset_userdata('permission');
$this->session->unset_userdata('logged_in');
$this->session->unset_userdata('session');
$this->session->unset_userdata('userinfo');
$this->session->unset_userdata('api_loc');
$this->session->sess_destroy();
redirect('signin');
}
.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Try adding a ? at the end of index.php in your last rewrite rule.
RewriteRule ^(.*)$ ./index.php?/$1 [L,QSA]
If that doesn't work, try changing your config.php 'uri_protocol' if it's set to AUTO.
$config['uri_protocol'] = 'REQUEST_URI';
if(){
redirect('Login');
}
I have a condition above that redirect the page to log in depending on some values. I can see in url that I get redirect because when I enter
http://www.myapp.com it changes to http://www.myapp.com/Login
Now In my route I have:
$route['default_controller'] = 'welcome';
$route['Login'] = 'login';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
My Login controller looks like:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('session');
$this->load->model('Login_model');
}
public function index(){
$data['title'] = 'GYM';
$this->load->view('login',$data);
}
}
In my view I have
But why Im getting
Object not found!
The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.
If you think this is a server error, please contact the webmaster.
Error 404
www.myapp.com
Apache/2.4.33 (Win32) OpenSSL/1.1.0h PHP/7.2.7
I cant figure out where I got configuration wrong
Update:
change config file:
$config['base_url'] = 'http://'.$_SERVER['HTTP_HOST'].'';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';//'AUTO';
$config['url_suffix'] = '';
$config['language'] = 'english';
$config['charset'] = 'UTF-8';
$config['enable_hooks'] = FALSE;
$config['subclass_prefix'] = 'MY_';
$config['composer_autoload'] = FALSE;
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-';
htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
and
RewriteEngine on
RewriteCond $1 !^(index\.php|[Javascript / CSS / Image root Folder name(s)]|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
accessing:
http://www.myapp.com/index.php/Login
Works but all the above does not. I not getting why
Try including below lines on the .htaccess at the root level of the codeigniter app folder :
RewriteCond $1 !^(index\.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?/$1 [L,QSA]
change your value like following
$route['login'] = 'Login';
$config['base_url'] = 'http://'.$_SERVER['HTTP_HOST'].'/';
if not work, replace this code in htaccess
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
After I remove index.php, I enable query string in codeigniter . But I have some trouble with redirect link. Detail , I have login form (login/index) and when login success redirect to "welcome/index" and save email in session .
But when login success only load view of "welcome/index" and wrong link , now link is :
"?login/index" And session don't save . Please help me .
Here's my code
Login.php (Controller)
if($this->input->post('email') != '' && $this->input->post('password') != ''){
if ($this->user->CheckLogin($this->input->post('email'),$this->input->post('password')) == true)
{
$this->load->library('session');
$this->session->set_flashdata('email', $this->input->post('email'));
redirect('welcome/index', 'refresh');
}
else
{
$data['error_message'] = '* Email or Password is incorrect';
$this->load->view('login',$data);
}
}else{
$this->load->view('login',$data);
}
Welcome.php (Controller)
class Welcome extends CI_Controller {
public function index()
{
$this->load->model('user');
$this->load->helper('url');
$this->load->library('session');
$data['email'] = $this->session->flashdata('email');
$this->load->view('welcome_message',$data);
}
}
welcome_message.php (View)
<?php
// Cant print email
echo $email;
?>
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
If you enable Query Strings then change the redirect like this
redirect('c=welcome &m=index', 'refresh');
Additionally it may required to write index.php? or only ? before c
Or if change configuation in config.php then you need change the c and m
like
$config['enable_query_strings'] = FALSE;
$config['controller_trigger'] = 'c';
$config['function_trigger'] = 'm';
for more info http://www.codeigniter.com/user_guide/general/urls.html#enabling-query-strings
I'm new to MVC and Codeigniter, but I have something working although not as I'd like and wondered if someone could help me?
I have 2 pages in my website (league, players) in a codeigniter sub-directory and currently my URL to get to them are 'http://www.mydomain.co.uk/codeigniter/index.php/golf' and 'http://www.mydomain.co.uk/codeigniter/index.php/players'
1) How to I remove the index.php from the URL? I've tried $config['index_page'] = ''; in config/config.php and set the .htaccess file, but no luck.
2) I only want to point my default controller in routes.php to golf controller and let the controller handle whatever page is being asked for.
3) Have I set this up correctly or if not then what is the proper way? One controller OK?
.htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ codeigniter/index.php/$1 [L]
config/routes.php
$route['default_controller'] = 'golf';
$route['players'] = 'golf/players'; <-Don't really want this entry!
config/autoload.php
$autoload['libraries'] = array('database');
$autoload['helper'] = array('url');
controllers/golf.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Golf extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$this->load->model('league_model');
$data['league'] = $this->league_model->get_League();
$data['title'] = 'League Table';
$this->load->view('templates/header', $data);
$this->load->view('templates/menu');
$this->load->view('league', $data);
$this->load->view('templates/footer');
}
public function players() { //Runs because of entry in config/routes.php
$route['players'] = 'golf/players';
$data['title'] = 'Players';
$this->load->view('templates/header', $data);
$this->load->view('templates/menu');
$this->load->view('players', $data);
$this->load->view('templates/footer');
}
}
?>
models/league_model.php
<?php
class League_model extends CI_Model {
public function __construct() {
}
public function get_League() {
$this->db->from("player");
$this->db->order_by("name", "asc");
$query = $this->db->get();
return $query->result_array();
}
}
?>
views/league.php
<p><?php echo $title; ?></p>
<?php foreach ($league as $item): ?>
<p><?php echo $item['name']." : ".$item['handicap']." : ".$item['bbnetbirdie']." : ".$item['bb4p'] ?></p>
<?php endforeach ?>
views/players.php
<p>This is players</p>
You .htaccess should look like this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase codeigniter/
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
And in config $config['index_page'] = ''; this is perfect
A simple .htaccess file i use :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
and on the config.php file, removing any reference to index.php
$config['index_page'] = ''
I have codeigniter installed in root/igniter folder.
under root/igniter, I have the following .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]
Pages open just as I would expect them to without using 'index.php'.
So, my home page lies at http://example.com/igniter and my registration page at http://example.com/igniter/registration opens as well.
However, in my registration form, the submit button which is handled by a CI controller redirects to
http://example.com/igniter/index.php/registration
I have made sure that my config.php under applications/config has
$config['index_page'] = "";
This is what my Registration controller looks like:
public function index()
{
$this->load->helper(array('form', 'url'));
$this->load->model('Registration_Model');
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]|callback_username_check|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[6]|max_length[12]|matches[passconf]');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|callback_email_check');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('registration');
}
else
{
//input data into database here
$username = strtolower($_POST['username']);
$password = md5($_POST['password']);
$email = strtolower($_POST['email']);
$data = array
(
'username' => $username,
'password' => $password,
'email' => $email
);
$this->Registration_Model->addUser($data);
$this->load->view('registrationsuccess');
}
}
As far as I can see, I am not explicitly asking the controller to redirect to index.php/registration.
Use this in your .htaccess file:
RewriteEngine on
RewriteCond $1 !^(index\.php|robots\.txt|images|css|js|user_guide)
RewriteRule ^(.*)$ /index.php/$1 [L]
Note the RewriteRule line in my code. There is no ? mark as you used. In RewriteCond you may use as many folders as you like and the mentioned folders have access. CodeIgniter can not access the resource from any folder that is not listed in RewriteCond here.
You should also point your index method at the method you want to receive it. For example, if your class is "Registration" your index method should look like this:
public function index()
{
this->registration();
}
Then do everything in the method called "registration." This will help keep things clear for other developers who might work on this code in the future.