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.
Related
I'm having Laravel build images for me, but once the image exists, I'd like Apache to serve it (instead of booting Laravel, reading in the image, and passing it through). Here is the rule I wrote in htaccess to do this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/storage/product_images/(.*).png
RewriteRule ^storage/product_images/(.*)\.png$ index.php/image/$1 [L]
My question is about this last bit: index.php/image/$1 in which I am trying to pass values into Laravel as though it were a route. This doesn't seem to be working. I'm just getting a 404 from Laravel even though the actual route works (/image/23). Is there a different way to do this? Maybe something like this?
index.php?_url=image/23
... another way to phrase this question: how might I use Laravel without an .htaccess? How could I simply pass params to the index.php file?
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
I'm having a strange problem. I recently moved my Laravel site from one directory to another in my localhost (on a Mac). Nothing has been right since. I had to change my .htaccess from the standard Laravel 4 .htaccess to:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
which is the alternative Laravel .htaccess for L4. That at least makes my public folder not be forbidden. But now Laravel just completely ignores the routes file. In fact, all routes (those in the file and those not) seem to valid and none seem to call a 404 error, even though I try to call both routes that exist and routes that don't. I even deleted everything in the routes file except the base route and nothing changed.
How is it possible that routes that don't exist can be called and I don't get the page not found error? All I get is a white screen. The same is true except for the base route. Why is it honoring only the base route (which is '')? If I change that route to one of the other controllers, it will call that controller. In short, the only route that works is ''. Why has everything changed since I moved the site?
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!
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.