how to change domain/controller/method/parameter to domain/parameter? - php

actually i just wanna make this only for 1 controller is UserController.php.
<?php
use \application\controllers\MainController;
class UserController extends MainController{
function __construct(){
$this->setsession();
$this->model('user');
}
public function index($username){
if (!isset($_SESSION['clogin'])){
$this->template1('index');
}else{
$query = $this->user->selectWhere(array('username'=>$username));
$data = $this->user->getResult($query);
$this->template2('user', $data);
}
}
}
?>
according to code above, to access user page i need to go to localhost/web/user/index/username i want change to localhost/web/username. I have try this in .htaccess
RewriteRule /([a-zA-Z0-9]+)/$ /user/index/$1
base on question here, but it's not working. this is the complete code of .htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
RewriteRule /([a-zA-Z0-9]+)/$ /user/index/$1
</IfModule>

Related

calling function from view in codeigniter not working

I am calling controller function from view with base url it's going to that function but showing 404 Page Not Found.
.htaccess I am using is this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
</IfModule>
config.php
$config['index_page'] = '';
autoload is
$autoload['helper'] = array('form', 'url', 'security');
default controller in routes.php is
$route['default_controller'] = 'prog_bar';
base_url is
$config['base_url'] = 'http://localhost:8080/jsLearning/prog_bar/';
view
a href="<?php echo base_url().'file_func'; ?>">abc</a>
controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class prog_bar extends CI_Controller {
public function index()
{
$this->load->view('prog_bar_view');
}
public function file_func(){
echo "form";
}
}
what else I am missing?
Class name should be started with capital letter,
Change this
class prog_bar extends CI_Controller {
to
class Prog_bar extends CI_Controller {
Your base_url is wrong. It should be
$config['base_url'] = 'http://localhost:8080/jsLearning/';
and now in HTML use site_url() instead base_url() like
abc
base_url() should be used only for resources link like image file, css file etc
Update your .htaccess by following
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I think you should be great with settings below:
In Config.php:
$config['base_url'] = 'http://localhost:8080/jsLearning/';
In Your View:
abc
In Your Controller:
class Prog_bar extends CI_Controller {
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
If you have still error, check to see what your base_url link to..Create a link
Base Url
to see in your browser where it links.
You do not use the base url for links within the site. Use the controller name and the methods name
<?php echo anchor("Pages/entry", "Post Entry"); ?>
What is form? It is not defined. Is it a page? if so, you have no view address for it. If its a snippet, you still need an address
$this->load->view('prog_bar_view')
a function will not simply show a view or snippet without being told where it is. If you are trying to include a form in a page, then just use nested views and in your main(html) view load the view you are trying to show with $this->load->view()

CodeIgniter setting up routing properly

I'm trying to setting up my codeigniter routing to make an authentication form for an admin panel, but the routing is not working. I'm a beginner with CodeIgniter and I think I'm missing something.
In my server I put the CodeIgniter fiels inside folder in my root directory, so it can be accessed like this:
/localhost/ci
In my config.php I set my base url and remove the index page to remove index.php:
$config['base_url'] = 'http://localhost/ci/';
$config['index_page'] = '';
Also I have edited the .htaccess to remove the index.php like CodeIgniter documentation says, so it looks like this:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Now, my routes config file looks like this. I set my default page and a route to my Auth controller:
$route['default_controller'] = 'auth';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['auth/login'] = "auth/login";
This is my Controller:
class Auth extends CI_Controller {
function __construct() {
parent::__construct();
// load libraries
}
function index() {
if (! $this->ion_auth->logged_in()) {
// redirect them to the dashboard page
redirect(base_url('auth/login'), 'refresh');
} else {
// show the dashboard page
redirect(base_url('dashboard'), 'refresh');
}
}
function login() {
$this->load->view('login');
}
When I enter in the web-browser http://localhost/ci/ it redirects me to http://localhost/ci/auth/login, but then a 404 Not Found error ir raised. What am I missing? I'm a bit confused at this point, thanks.
Try changing your .htaccess from:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
To
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /ci/index.php/$1 [L]

.htaccess throws a 500 error

I am using CodeIgniter and need to get rid of the "index.php" in the URL.
I copied the .htaccess file from codeigniter tutorial, emptied the $config['index_page'] = '';
and put the .htaccess in the root directory.
Without the .htaccess file it works fine, but ugly: www.example.com/index.php/site/home
For some reason it's not working and just throws a 500's error.
Where is the bug and how can I resolve it?
Thank you in advance
My .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Removes access to the system folders by users.
#Additionly this will allow you to create a System.php controller,
#previously this would not have not been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^{.*)$ /index.php?/$1 [L]
#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php/$1 [L]
#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>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php? and everything works as normal.
# submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
My site controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Site extends CI_Controller {
public function index(){
$this->home();
}
public function home(){
echo "default function started.<br/>";
$this->hello();
$data['title'] = 'Home!';
$data['name'] = 'Ilia';
$data['fname'] = 'Lev';
$data['operation'] = 'minus';
$data['val1'] = 5;
$data['val2'] = 7;
echo $data['operation']."<br/>";
$data['result'] = $this->math($data);
$this->viewLoad("home_view", $data);
echo "<br/>";
}
public function hello(){
echo "hello function started.<br/>";
}
public function math($data){
$operation = $data['operation'];
$val1 = $data['val1'];
$val2 = $data['val2'];
$this->load->model("math");
return $this->math->calculate($operation, $val1, $val2)."<br/>";
}
public function viewLoad($whatToView, $data){
try{
$this->load->view($whatToView, $data);
}catch(Exception $e){
echo "There is no such view";
}
}
public function about(){
$data['title'] = "About!";
$this->viewLoad("about_view",$data);
}
}
If this is really part of the .htaccess file, it would cause your problem:
RewriteRule ^{.*)$ /index.php?/$1 [L]
^^^ typo?
Should be:
RewriteRule ^(.*)$ /index.php?/$1 [L]
I too had the same problem but my code was correct and i was using wamp server, then
I found rewrite module is not enabled
so went to apache/conf/httpd.conf and uncommented line
LoadModule rewrite_module modules/mod_rewrite.so
Now its working fine.

500 error thrown due to .htaccess bug [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
.htaccess throws a 500 error
I am using CodeIgniter and need to get rid of the "index.php" in the URL.
I copied the .htaccess file from CI tutorial, emptied the $config['index_page'] = '';
and put the .htaccess in the root directory.
Without the .htaccess file it works fine, but ugly: www.example.com/index.php/site/home
For some reason it's not working and just throws a 500's error.
In the log there is a row that repeats itself in every attempt: [Thu
Dec 20 04:01:13 2012] [alert] [client 2.55.118.161]
/home/morro1980/data/www/knight-guard.org/.htaccess:
directive requires additional arguments.
The mod_rewrite is installed and working
Where is the bug and how can I resolve it?
Thank you in advance
My .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Removes access to the system folders by users.
#Additionly this will allow you to create a System.php controller,
#previously this would not have not been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php/$1 [L]
#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>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php? and everything works as normal.
# submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
My site controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Site extends CI_Controller {
public function index(){
$this->home();
}
public function home(){
echo "default function started.<br/>";
$this->hello();
$data['title'] = 'Home!';
$data['name'] = 'Ilia';
$data['fname'] = 'Lev';
$data['operation'] = 'minus';
$data['val1'] = 5;
$data['val2'] = 7;
echo $data['operation']."<br/>";
$data['result'] = $this->math($data);
$this->viewLoad("home_view", $data);
echo "<br/>";
}
public function hello(){
echo "hello function started.<br/>";
}
public function math($data){
$operation = $data['operation'];
$val1 = $data['val1'];
$val2 = $data['val2'];
$this->load->model("math");
return $this->math->calculate($operation, $val1, $val2)."<br/>";
}
public function viewLoad($whatToView, $data){
try{
$this->load->view($whatToView, $data);
}catch(Exception $e){
echo "There is no such view";
}
}
public function about(){
$data['title'] = "About!";
$this->viewLoad("about_view",$data);
}
}
The first <ifModule> is not closed, this:
RewriteRule ^(.*)$ index.php?/$1 [L]
<IfModule>
has to be
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
#^
Make sure you have <IfModule></IfModule> matched in your .htaccess.

.htaccess not acting as suspected

I'm using CI and WAMP on a Windows 7 machine. In my Application folder of the site I have a .htaccess file with the following:
RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ /index.php/$1 [L]
This was copied and modified CodeIgniter User Guide. And I made sure the rewrite_module (didn't it used to be mod_rewrite?) was active and restarted WAMP. In my controller, I have the following:
<?php
class Forum extends CI_Controller
{
public function index()
{
$data['title'] = "Card Forums";
$this->load->view('Header', $data);
$this->load->model('forumdb');
$data['sections'] = $this->forumdb->getSections();
$this->load->view('Home', $data);
$this->load->view('Footer');
}
public function site()
{
$data['title'] = "Card Forums - Site";
$this->load->view('Header', $data);
$data['section'] = "Site";
$this->load->view('Forum', $data);
$this->load->view('Footer');
}
}
When I go to localhost/forum it does exactly as I want. But when I try to go to localhost/forum/site it is displaying the following:
Not Found
The requested URL /forum/site was not found on this server.
In the view Forum I have:
<?php echo $section; ?>
Which is only supposed to display the the variable right now because I want to make sure I'm accessing the page first (just started writing this site Friday). When I try localhost/forum/index.php/site it's giving me a 404 error.
I have a feeling I'm missing something VERY simple here and it's bugging me. Any help would be appreciated.
Here is an updated .htaccess file
RewriteEngine on
RewriteCond $1 !^(index\.php|css)
RewriteRule ^(.*)$ index.php/$1 [L]
In your /forum/config folder find the config.php and change this line
$config['index_page'] = 'index.php';
To
$config['index_page'] = '';
Then as you mentioned you can use function like base_url('css/site.css');
One thing I generally do with this type of htaccess file is add an entry for a folder called public so in my case it would look like this.
RewriteEngine on
RewriteCond $1 !^(index\.php|public)
RewriteRule ^(.*)$ index.php/$1 [L]
Then I have all my assests in the public folder, and I don't need to keep adding exceptions to my htaccess file.
For WAMP (and most other environemnts for my CI projects), I've had most luck with the following additional flags in .htaccess:
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [NC,L,QSA]
Also, Check your uri_protocol in config.php. I've had most luck with setting it to PATH_INFO instead of AUTO.
One last thing to try: CI sometimes detects the base_url wrong in WAMP environments. Either define your base_url in config.php.

Categories