Welcome Guest, Not a member yet? Register   Sign In
Order resource route in routes.php
#1

Hello,

In the manual of Codeigniter 4 stands:
The routes are matched in the order they are specified, so if you have a resource photos above a get ‘photos/poll’ the show action’s route for the resource line will be matched before the get line. 

When trying this the recource is working/showing in the end, not on the place I put it in.

Sor when you add something like this in your routes.php:

$routes->resource('photos');

$routes->add('a', 'Home::a');
$routes->add('b', 'Home::b');
$routes->add('c', 'Home::c');

$routes->add('(:any)', 'Home::i/$1');

http://www.example.com/photos gives a "File not found"
And the recource Photos.php when looked up in the debugbar will have the last place in the ordering instead of the first place.

Why is this?
And how to change this?

Thank you for your help.

Ubel Jan van Wijhe
Reply
#2

(This post was last modified: 07-14-2018, 07:13 AM by John_Betong.)

Try adding a forward slash because it works OK for me:

PHP Code:
$routes->add('/a''Home::a');
$routes->add('/b''Home::b');
$routes->add('/c''Home::c');

$routes->add('/(:any)''Home::i/$1'); 

From Routes.php
Quote:**
 * --------------------------------------------------------------------
 * Route Definitions
 * --------------------------------------------------------------------
 */
 
// We get a performance increase by specifying the default
// route since we don't have to scan directories.
# $routes->add('/', 'C_Home::index');
# $routes->add('/home', 'C_Home::index');
Reply
#3

To clarify and wonder.

Try the following situation.

PHP Code:
/**
 * --------------------------------------------------------------------
 * Route Definitions
 * --------------------------------------------------------------------
 */

// We get a performance increase by specifying the default
// route since we don't have to scan directories.
$routes->add('/''Begin::index');

//  resource
$routes->resource('test');
$routes->resource('test2', ['namespace' => 'App\Controllers\Admin']);

//  Static visitor
$routes->add('a''Begin::a');
$routes->add('b''Begin::b');
$routes->add('c''Begin::c');

//  Dynamic visitor
$routes->add('(:any)''Begin::i/$1');
$routes->add('(:any)/(:any)''Begin::i2/$1/$2'); 

1) The recources are loaded after the wildcards
The order above is clearly before
How to change this?

2) The Uri /admin/test/i  will not work because of the wildcards.
( The controller is there and works with no wild card present. )

Only the wild cards only cover 2 segments not 3.
In Codeigniter 3 this works, but not in Codeigniter 4?

3) Adding slashes didn't change anything.
The adding of leading slashes is even discouraged when reading a couple forum threads.

So even more questions or are these bugs?

Greetings,
Ubel Jan van Wijhe
Reply
#4

(07-15-2018, 01:13 AM)ubeljan Wrote: To clarify and wonder.

Try the following situation.

PHP Code:
/**
 * --------------------------------------------------------------------
 * Route Definitions
 * --------------------------------------------------------------------
 */

// We get a performance increase by specifying the default
// route since we don't have to scan directories.
$routes->add('/''Begin::index');

//  resource
$routes->resource('test');
$routes->resource('test2', ['namespace' => 'App\Controllers\Admin']);

//  Static visitor
$routes->add('a''Begin::a');
$routes->add('b''Begin::b');
$routes->add('c''Begin::c');

//  Dynamic visitor
$routes->add('(:any)''Begin::i/$1');
$routes->add('(:any)/(:any)''Begin::i2/$1/$2'); 

1) The recources are loaded after the wildcards
The order above is clearly before
How to change this?

2) The Uri /admin/test/i  will not work because of the wildcards.
( The controller is there and works with no wild card present. )

Only the wild cards only cover 2 segments not 3.
In Codeigniter 3 this works, but not in Codeigniter 4?

3) Adding slashes didn't change anything.
The adding of leading slashes is even discouraged when reading a couple forum threads.

So even more questions or are these bugs?

Greetings,
Ubel Jan van Wijhe

Here is the important stuff in config/Routes.php

And the Online Temporary Test Site I am currently working. I have not been able to get Pagination to work so created my own - which leaves a lot to be desired Sad

https://johns-jokes.cf/

PHP Code:
$routes->setDefaultNamespace('App\Controllers');
# $routes->setDefaultController('C_Todo');
$routes->setDefaultController('C_Base');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override();
$routes->setAutoRoute(true);
$routes->discoverLocal(false);

/**
 * --------------------------------------------------------------------
 * Route Definitions
 * --------------------------------------------------------------------
 */
 
// We get a performance increase by specifying the default
// route since we don't have to scan directories.
# $routes->add('/',                             'C_Home::index');
# $routes->add('/home',                     'C_Home::index');


$routes->add('/base'      'C_Base::index');
$routes->add('/home'      'C_Home::index');
$routes->add('/welcome'   'C_Home::welcome');

$routes->add('/todo'      'C_Todo::index');
$routes->add('/'          'C_Todo::index');
$routes->add('/blogs',         'C_Blogs::index');

$novels = [
 
 'revenge-season',
 
 'fear-and-loathing-in-pattaya',
 
 'general-trinh-is-delivering-papers',
];
foreach(
$novels as $novel):
 
 # http://localhost/revenge-season?page=3
 
 $routes->add('/'.$novel .'/(:any)''C_Novels::chapter');
 
 $routes->add('/'.$novel           'C_Novels::novel');
endforeach; 
 
$routes
->add('/novels'    'C_Novels::index');

$routes->add('/ambasense' 'C_Whoops::ambasense');

# CATCHALL
$routes->add('/(:any)',            'C_Whoops::index'); 
Reply
#5

(07-15-2018, 02:39 AM)John_Betong Wrote:
(07-15-2018, 01:13 AM)ubeljan Wrote: To clarify and wonder.

Try the following situation.

PHP Code:
/**
 * --------------------------------------------------------------------
 * Route Definitions
 * --------------------------------------------------------------------
 */

// We get a performance increase by specifying the default
// route since we don't have to scan directories.
$routes->add('/''Begin::index');

//  resource
$routes->resource('test');
$routes->resource('test2', ['namespace' => 'App\Controllers\Admin']);

//  Static visitor
$routes->add('a''Begin::a');
$routes->add('b''Begin::b');
$routes->add('c''Begin::c');

//  Dynamic visitor
$routes->add('(:any)''Begin::i/$1');
$routes->add('(:any)/(:any)''Begin::i2/$1/$2'); 

1) The recources are loaded after the wildcards
The order above is clearly before
How to change this?

2) The Uri /admin/test/i  will not work because of the wildcards.
( The controller is there and works with no wild card present. )

Only the wild cards only cover 2 segments not 3.
In Codeigniter 3 this works, but not in Codeigniter 4?

3) Adding slashes didn't change anything.
The adding of leading slashes is even discouraged when reading a couple forum threads.

So even more questions or are these bugs?

Greetings,
Ubel Jan van Wijhe

Here is the important stuff in config/Routes.php

And the Online Temporary Test Site I am currently working. I have not been able to get Pagination to work so created my own - which leaves a lot to be desired Sad

https://johns-jokes.cf/

PHP Code:
$routes->setDefaultNamespace('App\Controllers');
# $routes->setDefaultController('C_Todo');
$routes->setDefaultController('C_Base');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override();
$routes->setAutoRoute(true);
$routes->discoverLocal(false);

/**
 * --------------------------------------------------------------------
 * Route Definitions
 * --------------------------------------------------------------------
 */
 
// We get a performance increase by specifying the default
// route since we don't have to scan directories.
# $routes->add('/',                             'C_Home::index');
# $routes->add('/home',                     'C_Home::index');


$routes->add('/base'      'C_Base::index');
$routes->add('/home'      'C_Home::index');
$routes->add('/welcome'   'C_Home::welcome');

$routes->add('/todo'      'C_Todo::index');
$routes->add('/'          'C_Todo::index');
$routes->add('/blogs',         'C_Blogs::index');

$novels = [
 
 'revenge-season',
 
 'fear-and-loathing-in-pattaya',
 
 'general-trinh-is-delivering-papers',
];
foreach(
$novels as $novel):
 
 # http://localhost/revenge-season?page=3
 
 $routes->add('/'.$novel .'/(:any)''C_Novels::chapter');
 
 $routes->add('/'.$novel           'C_Novels::novel');
endforeach; 
 
$routes
->add('/novels'    'C_Novels::index');

$routes->add('/ambasense' 'C_Whoops::ambasense');

# CATCHALL
$routes->add('/(:any)',            'C_Whoops::index'); 

The wildcard problem is solved by using regular expressions :

PHP Code:
//  Dynamic visitor
$routes->add('([a-z0-9_]+)''Begin::i/$1');
$routes->add('([a-z0-9_]+)/([a-z0-9_]+)''Begin::i2/$1/$2'); 

And after that  ./admin/test/i  also works.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB