jquery.ajax in codeigniter - codeigniter

I am having a problem in implementing jquery.ajax in codeigniter. I want to send the control to a specific function of a controller. I am setting the url in my javascript function like this
var url='<?php echo('First/index');?>';
var ajaxoptions={url:url,success:submit_ajax_response};
First is my controller and index is my function where I want to send the control. When I click on the event on which it is called the following url is formed
http://localhost/codeigniter/First/index
The URL is fine but it is generating the error of 404. I have done such kind of operations various times in zendframework but unable to accomplish this job in codeigniter. I have noticed one thing that if I add index.php in the url it works fine. By adding index.php the url becomes like
http://localhost/codeigniter/index.php/First/index
I am astonished how to remove index.php from route file. I have only two lines in route.php file
$route['default_controller'] = "First";
$route['404_override'] = '';
I have already made my controller as the default controller.
Am I doing correct? What is the problem and how to accomplish this job`

You need to check a couple of things. First, in /application/config/config.php make sure index file is set to this:
$config['index_page'] = '';
Second, make sure you have correct .htaccess. This should be at the root of your public directory (same place as your index.php):
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
Options FollowSymLinks
#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>

I notice the part that is missing form your URL is the "index.php" thing that CodeIgniter has.
Change your code to this: (You need URL helper, so load that before this):
var url="<?php echo (index_page() . 'First/index');?>";
var ajaxoptions={url:url,success:submit_ajax_response};
index_page returns your site "index" page, as specified in your config file.
In order to remove index.php from your CodeIgniter links see here.

Related

rewrite url only and stay on the same page

I have read a lot on stack about rewriterule and how it applies and I've tried reading up on some good articles online but I still cannot wrap my head around a few things.
I have blogs setup where all folders are in
https://domain.ca/posts/post-tree/*
So I've setup htaccess like this
RewriteRule ^posts/post-tree/(.*)$ /index.php?$1 [R=301]
As I'm sure you can guess this basically brings me root index.php where I catch this request with a $_GET to know the name of the blog folder it was requesting.
This is fine I can hit index.php and with $_GET I know the blog page they requested.
What I do not get, and I've tried a lot of things, is once I have this request in index.php how do I re-write the URL to show something like https://domain.ca/blogpage/ instead of looking like https://domain.ca/index.php? where https://domain.ca/blogpage/ does not really exist of course, but it is because I want to hide the http://domain.ca/posts/post-tree/ path.
Its a little like when wordpress processes a blog page with the id and after rewrites the url to whatever slug is set for that blog page. at least my understanding of it as they don't have individual folders for blogs, but I do.
I finally got this working with the following in the htaccess file
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# the above checks if file or folder exists, if not the below is processed
# this will route to base index file and fetch $1 folder via $_GET
RewriteRule . /posts/post-tree/index.php?$1

how to change standard URL in codeigniter for multilanguage?

I have a multi language website in code igniter with uses prefix in the URL to define the language. Works great but there is a problem when going to the home page.
The default URL is
localhost:8888
but it should be
localhost:8888/index.php/EN/welcome
I tried redirecting in the controller but that didn't work.
Any ideas on how to fix this problem?
Thanks a lot
First of all you must create a .htaccess file in your root path. File content like this;
Options -Indexes
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
You can edit your route file like this;
$route['(:any)/(:any'] = 'IndexController/getPage/$1/$2';
$route['(:any)'] = 'IndexController/index/$1';
$route['default_controller'] = 'IndexController';
Route 1: Your sub page. First parameter is language, second parameter is page url
Route 2: Your home page. Parameter is language.
Route 3: Default home page in your main language.

CodeIgniter: keep index.php in url and hide the rest

greatings...
Question :
How to keep index.php in url and hide everything else so my url look like (www.base_url.com/index.php) for every page?
Details :
i've searched but i only got how to remove index.php, i think im the only one who want to keep it...lol
im using codeigniter 3 to working on web based app human resource managemen soo i dont need SEO url since the app only accessed by internal employees. i like to make the url clean, only show base_url and the index.php for every page. please show me how to config codeigniter like i need...thank you
Use .htaccess like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
This will look more cleaner than with index.php. The URL will be like this www.your_baseurl.com/users
and edit your config.php file to use URI like this:
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
You can use your route like this:
$route['users'] = 'home/users';

Custom route not working in codeigniter

I believe that I already did all the important thing to set up the custom route in codeigniter but I still don't know why I always get 404 error.
The current url I can access is: http://localhost:8080/project/api/profile_test/
And I want to rewrite it as: http://localhost:8080/project/api/users/
I've added this code in the route.php but still not working: $route[‘users’] = 'profile_test';
My .htaccess is:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [L]
I already set the AllowOverride All and remove the index.php in $config['index_page'] = '';. But still not working.
I’m almost 8 hours for this with this little thing and I need help to the one who knows it.
What am I missing? Any help is much appreciated.
In Codeigniter, a route consists of 3 parts:
the URL
the controller name
the method name
The docs explain it well.
So, if your project's root is http://localhost:8080/, for a URL like project/api/users/, to be handled by a controller (located in application/controllers/) called Profile_test and a method called index, you would need a route like:
$route['/project/api/users'] = 'profile_test/index';
Note that the controller file and class must be capitalised, as described in the docs, so for example controllers/Profile_test.php.
If your project's root is http://localhost:8080/project, you would change that to:
$route['/api/users'] = 'profile_test/index';
If your controller is located in a subdirectory like application/controllers/api/, you would change that to:
$route['/project/api/users'] = 'api/profile_test/index';
Also make sure you are using normal single quotes. Some of the code you include in your question includes 'smart' quotes: $route[‘users’] which will not work in PHP.

CodeIgniter Only Index Page is working

I recently started using codeIgniter and I am stuck in a problem.
I've two files in my view folder
index.php
profile.php
When I go to the url http://localhost/php/ci/index.php/ it shows my index.php page and everything is fine.
But when I go to http://localhost/php/ci/profile.php/ it says
The requested url was not found on server.
Why is this happening what's the problem ??
My Controller Files Names Are:
home_control.php
profile_control.php
home_control will interact with index.php and profile_control will interact with profile.php.
You have a fundamental flaw in your MVC understanding. The index.php file sitting in your document root (that is, your www directory) is actually responsible for getting CodeIgniter started on every request. That is the index.php that you are seeing in the URL bar.
You are confusing said index.php with the index.php that you fabricated inside of the views folder. Never will you be able to create a file inside of your views folder and be able to immediately access it using the URL bar. You must go through a controller.
If you access http://localhost/php/ci/index.php/profile_control and controllers/profile_control.php contains::
<?php
class Profile_control extends CI_Controller{
function __construct(){
parent::__construct();
}
function index(){
$this->load->view('profile')
}
}
You will be able to see the contents inside of views/profile.php
To reduce confusion, it is essential that you read this before going forward.
That is the wrong way to do it, read the manual about how to setup your .htaccess file and how routing works. You don't need to have multiple index.php-like files, the should only be one.
Here is the link from where you should start reading.
Like the other said. Everything goes through index.php. But you can actually hide it so that you don't have to type it in the url, which is not pretty btw.
Go to your applications/config/config.php and find something like the following:
/*
|--------------------------------------------------------------------------
| Index File
|--------------------------------------------------------------------------
|
| Typically this will be your index.php file, unless you've renamed it to
| something else. If you are using mod_rewrite to remove the page set this
| variable so that it is blank.
|
*/
$config['index_page'] = "index.php";
remove the index.php. And in the .htaccess (which is next to your index.php) you need to add this If I'm not mistaken:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
If everything works you can call your profile controller like http://localhost/php/ci/profile_control. By default it calls the index action so make sure you have an public function index in that controller. The url structure is always http://url.com/*controller*/*action*/*extra params here*
If you wish to call another action (function) inside your controller, for example public function profile(), you can call this url http://localhost/php/ci/profile_control/profile.
It is in that second action that you specify your other view file with $this->load->view('profile'). That will call the view/profile.php file
You can also pass values to that action like this -> http://localhost/php/ci/profile_control/profile/id/7.
in your profile action you need to grab those values as follow
public function profile($action, $value)
{
//$action = the word id and $value = the number 7
}
You can create another .htaccess file inside the project folder but outside application folder with the following code
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I also had the same issue where only my index page was working and It worked for me!

Resources