• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
route value can be used in URL

#1
Hi,

I've just discovered that the route value can be used in a url e.g. This route

Code:
$route['products/get/all'] = "main/getAllProducts";

can be run with example.com/products/get/all or example.com/main/getAllProducts


Is this by design?
Or, have I done something wrong?
Is there a way I serve a 404 if a user uses the route value?
Reply

#2
The only fix for this i could find about this issue is https://stackoverflow.com/a/35290306/8832064, more people struggle with this issue for indexation.
Reply

#3
(11-21-2020, 01:59 PM)superior Wrote: The only fix for this i could find about this issue is https://stackoverflow.com/a/35290306/8832064, more people struggle with this issue for indexation.

Thanks for replying.

This is how I addressed the issue.

I decided on a convention that the $route first segment should always be different than the class name to be called.

Then I update the class constructor to check the uri_string()

So.. $route['products/get/all'] = "main/getAllProducts";


PHP Code:
class Main extends CI_Controller
{
  public function __construct()
  {
    parent::__construct();
    $controller explode('/'uri_string())[0];
    if ($controller == 'main') {
      //-- take appropriate action
    

This does not work with cached pages as they don't go down this "route"  Cool but I'm okay with that.  Big Grin
Reply

#4
(Yesterday, 12:25 AM)paulkd Wrote:
(11-21-2020, 01:59 PM)superior Wrote: The only fix for this i could find about this issue is https://stackoverflow.com/a/35290306/8832064, more people struggle with this issue for indexation.

Thanks for replying.

This is how I addressed the issue.

I decided on a convention that the $route first segment should always be different than the class name to be called.

Then I update the class constructor to check the uri_string()

So.. $route['products/get/all'] = "main/getAllProducts";


PHP Code:
class Main extends CI_Controller
{
  public function __construct()
  {
    parent::__construct();
    $controller explode('/'uri_string())[0];
    if ($controller == 'main') {
      //-- take appropriate action
    

This does not work with cached pages as they don't go down this "route"  Cool but I'm okay with that.  Big Grin

Why would you rewrite this on every controller/method in your project?
Isn't it easier to change the core route class in example given on the Stackoverflow question?
This will save you tons of writing and when a change is required you could just modify a single class/method.

I would never write this kind of solutions on a Controller/Method, that's why we have MY_ to change core functionality.
Reply

#5
(Today, 03:23 AM)superior Wrote:
(Yesterday, 12:25 AM)paulkd Wrote:
(11-21-2020, 01:59 PM)superior Wrote: The only fix for this i could find about this issue is https://stackoverflow.com/a/35290306/8832064, more people struggle with this issue for indexation.

Thanks for replying.

This is how I addressed the issue.

I decided on a convention that the $route first segment should always be different than the class name to be called.

Then I update the class constructor to check the uri_string()

So.. $route['products/get/all'] = "main/getAllProducts";


PHP Code:
class Main extends CI_Controller
{
  public function __construct()
  {
    parent::__construct();
    $controller explode('/'uri_string())[0];
    if ($controller == 'main') {
      //-- take appropriate action
    

This does not work with cached pages as they don't go down this "route"  Cool but I'm okay with that.  Big Grin

Why would you rewrite this on every controller/method in your project?
Isn't it easier to change the core route class in example given on the Stackoverflow question?
This will save you tons of writing and when a change is required you could just modify a single class/method.

I would never write this kind of solutions on a Controller/Method, that's why we have MY_ to change core functionality.

Hi Superior,

You are correct. Most of my controllers are actually extended of a base controller in My_Controller.

So, although my example was extended off CI_Controller, in reality the hack is only used/required in a couple of constructors.  Big Grin
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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