Laravel not honoring the routes.php file - laravel

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?

Related

Laravel - Using htaccess to manually pass paramaters to index.php

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?

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

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.

Laravel infinite loop

I'm building an app using Laravel v.4.2. In the app/public directory, I have images folder which contains static images. Then, I created an ImagesController for users to manage assets. However, when I entered the URL: myapp.dev/images into browser, I got an infinite redirect loop error. I double checked all my routes and noticed there're no ones related to images path. Even when I commented out all routes entries, the error still exists.
I found a work around to this by rename the controller, however this is not going to be an ideal solution.
What can I do to completely deal with this error?
You will have to rename either your images directory, or your images route. What's happening is expected behavior of your server: when it hits the public directory (which is where all Laravel requests begin), it's finding that the images folder exists, so your URL is not redirected to index.php and your application is not launched. So it never even gets to your routes.
The cause of the loop itself depends on the contents of your .htaccess file, but it is probably happening because when you request myapp.dev/images, your server recognizes that images is a directory and immediately returns a 301 redirect to myapp.dev/images/ (with the trailing /). Then, your .htaccess file jumps in and tries to convert it back to myapp.dev/images, without the trailing slash. This happens in the line RewriteRule ^(.*)/$ /$1 [L,R=301].
Normally, when navigating to a folder in app/public, you should get a 403 Forbidden error. The line that does this is Options -Indexes, which disables the ability to display a list (or index) of directories in the browser. Usually, you have indexes disabled server-wide, in your server's httpd.conf file. You might want to check that that's the case—or at the very least, add Options -Indexes to your .htaccess file in app/public.
Also, make sure that your .htaccess file in app/public contains both of these lines:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

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