• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CodeIgniter Routing works on MAMP NOT LAMP

#11
[eluser]andyscraven[/eluser]
Hi Narf,

So I changed the name of the controller file to home.php but it failed when it tried to go to a product page.

I got the error:

Unable to load your default controller. Please make sure the controller specified in your Routes.php file is valid.

Routes has:

$route["$product/(:any)"] = "Home/productdetail/$1";

do I need to change everything to lowercase?

Thanks

#12
[eluser]Narf[/eluser]
You know ... some things will take less time if you just try them first.

#13
[eluser]andyscraven[/eluser]
...and you know Narf, some things take less time if the advice given isn't wrong?

As a perfect example of wrong advice! "Route configuration doesn’t matter, it’s the filename that matters - rename from Home.php to home.php and you’re done."

The most relevant wrong part being, "rename from Home.php to home.php and you’re done!"

Hence why I asked the above question as to clarify I understood "assuming" your expertise was greater than mine!

So, I ask again. Was your original advice wrong or do I not understand?

#14
[eluser]Tim Brownlaw[/eluser]
[quote author="andyscraven" date="1413676167"]...and you know Narf, some things take less time if the advice given isn't wrong?

As a perfect example of wrong advice! "Route configuration doesn’t matter, it’s the filename that matters - rename from Home.php to home.php and you’re done."

The most relevant wrong part being, "rename from Home.php to home.php and you’re done!"

Hence why I asked the above question as to clarify I understood "assuming" your expertise was greater than mine!

So, I ask again. Was your original advice wrong or do I not understand?
[/quote]

So what did you do to get it working?

#15
[eluser]andyscraven[/eluser]
Hi Tim,

Well I haven't yet, hence why I keep asking about it.

#16
[eluser]Tim Brownlaw[/eluser]
Well Andy...
I've just setup your case in my CI LAMP dev site and this is what I've come up with...
HINT: Always write out what you have done and what the result was! That way you know what you tried and what happened!
The Setup
In application/config/route.php we have
Code:
$route['default_controller'] = "home";
$route['404_override'] = '';
In application/controllers/home.php we have
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller {
  public function index()
  {
    $this->load->view('welcome_message');
  }
}
/* End of file home.php */
/* Location: ./application/controllers/home.php */
RESULT: When we run cidev22.com – we get the default Home Controller loading and we are greeted byt the CI Welcome Message... So that all works.
Now WHAT IF I set the Controller Class name from Home to home.
So
Code:
class Home extends CI_Controller {
becomes
Code:
class home extends CI_Controller {
RESULT: It still works. But class names should be capitalized!

Now What IF I change the Controllers Filename from home.php to Home.php
RESULT: It goes BANG – 404 Page Not Found

Now What IF I change the route from
Code:
$route['default_controller'] = 'home';
to
Code:
$route['default_controller'] = 'Home';
RESULT: It works – In this particular instance
NEXT:
NOW Let's set the router to include
Code:
$route['$product/(:any)'] = 'Home/productdetail/$1';

Now our controller becomes...
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller {
  public function index() {
  $this->load->view('welcome_message');
  }

  public function productdetail() {
    $product_name = $this->uri->segment(2);
    if (empty($product_name)) {
      echo 'No Product Selected';
    }
    else {
      echo 'You chose ' . $product_name;
    }
  }
}
/* End of file home.php */
/* Location: ./application/controllers/home.php */
And the application/config/routes.php becomes
Code:
$route['default_controller'] = 'home';
$route['product'] = 'home/productdetail';  // Required when only website.com/product is entered else you get a "Page Not Found"
$route['product/(:any)'] = 'home/productdetail/$1'; // Required for website.com/product/smurfs
$route['404_override'] = '';
IMPORTANT NOTE: It appears that the URL in a case like $route['product/(:any)'] = 'home/productdetail/$1'; IS Case Sensitive... so 'home/productdetail/$1'; Works whereas 'Home/productdetail/$1'; results in a 404 Page not found
It appears that $route['product'] is required to catch the default case. Well that's what happened in this test setup anyways...

Entering website.com/product results in
Code:
No Product Selected
Entering website.com/product/smurfs results in
Code:
You chose smurfs

So think through what you are doing... DO IT – Write down what you did and what happened and you'll end up finding the Answer!
Just like I did here and I even learned a few new things!!!

#17
[eluser]andyscraven[/eluser]
Hi Tim

Thank you for your detailed run through and I think that I did not explain myself correctly, for which I apologise.

The issue I have is that I am using MAMP for my local server, which I now realise I should change to a setup that matches my Centos Servers.

On my local machine the problem does not occur, I simply cannot replicate it but I have a ton of sites going through each of my servers that get constant traffic so am reluctant to play around and have broken sites, hence why I came to the forum in the hope that someone at Ellis Lab would know the problem and just put me right.

What came out of your trial and I was not able to test was the case sensitivity on routes such as,

$route["$product/(:any)"] = "Home/productdetail/$1";
$route["$product"] = "Home/productdetail";

and would explain that by fixing one issue I caused another.

I will wait for a quiet time on one of my servers and give that a go, as the results you got were exactly the error I received when it went wrong in the first place but not being able to replicate it locally and therefore run a test has been really frustrating.

Thanks again.


#18
[eluser]Tim Brownlaw[/eluser]
MAMP, like WAMP, is not case Sensitive when it comes to folders and file names whereas LAMP definitely is! Which is why you cannot "recreate it" on your MAMP Box as it doesn't care!

I did a quick cruise on google to see if you could force MAMP "to care" about case, but didn't come across anything of significance!

Another option might be to install something like Virtualbox ( or whatever Apple might have ) and install a centos server on that. It's pretty painless to do!

Your Reversed Routes is interesting...
Code:
$route[”$product/(:any)”] = “Home/productdetail/$1”;
$route[”$product”] = “Home/productdetail”;
What you have there is a case I didn't test for. So reversing the routes to how I originally had them still works, I just tested it! The only difference in my case is that my filename for my home.php controller is lower case! And yep in those routes Case Does Matter!

Anyway good luck with the testing. We've given you everything you need to sort this out!
Let us know how it goes!

Cheers
Tim

#19
[eluser]Narf[/eluser]
[quote author="andyscraven" date="1413676167"]...and you know Narf, some things take less time if the advice given isn't wrong?

As a perfect example of wrong advice! "Route configuration doesn’t matter, it’s the filename that matters - rename from Home.php to home.php and you’re done."

The most relevant wrong part being, "rename from Home.php to home.php and you’re done!"

Hence why I asked the above question as to clarify I understood "assuming" your expertise was greater than mine!

So, I ask again. Was your original advice wrong or do I not understand?
[/quote]

My original advice was admittedly, half-wrong (the configuration part apparently does matter, though not always ... CI2, a strange thing).
Still, you are not making any effort yourself. You haven't even read the documentation.

Tim Brownlaw's advice is apparently based on CI3-dev, which changes how routing works quite a bit. For CI2 use all lowercase.

#20
[eluser]andyscraven[/eluser]
Hi Narf,

I did read the docs for C12 and if you look under the Controllers section they do use an example of "blog," which is lowercase but they do not state that it must be lowercase.

In contrast they do however state further down that the Controller itself must start with Uppercase.

So I was not to know the former was also true.

I have the docs page pinned to my browser so they are always available.

The other issue is that I did not know that MAMP ignores case, very annoying, so could not reproduce it locally. Just downloaded Virtual Machine, so that does not happen again.

Is it worth upgrading to C13? Or should I research that myself :-)


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2017 MyBB Group.