Welcome Guest, Not a member yet? Register   Sign In
(:any) Route Placeholder Not Working?
#1

Documentation states that the (:any) placeholder should return the remainder of the URI string, but in my implementation, I only ever get the next segment. Everything after the proceeding / is truncated. Am I doing something wrong?

Routes.php -Route Definitions
Code:
$routes->get('auth/', 'Auth::index',['namespace' => 'IonAuth\Controllers']);
$routes->get('auth/login', 'Auth::login',['namespace' => 'IonAuth\Controllers']);
$routes->get('auth/logout', 'Auth::logout',['namespace' => 'IonAuth\Controllers']);
$routes->get('auth/forgot_password', 'Auth::forgot_password',['namespace' => 'IonAuth\Controllers']);

$routes->match(['get', 'post'], 'pages/create', 'Pages::create');
$routes->get('ajax/(:any)', 'Ajax::$1');
$routes->get('/', 'Pages::view');
$routes->get('(:any)', 'Pages::view/$1');

Pages->View() 
Code:
public function view($slug = null)
{
dd($slug);
}
 
If I enter a url like http://www.example.com/docs/tutorial

I would expect $slug to be "docs/tutorial"

[font=Tahoma, Verdana, Arial, sans-serif]Instead all I get is "docs";[/font]

[font=Tahoma, Verdana, Arial, sans-serif]Am I setting something up incorrectly? [/font]

[font=Tahoma, Verdana, Arial, sans-serif]Thanks![/font]
Reply
#2

(This post was last modified: 03-19-2020, 12:45 PM by jreklund.)

Can you link that part of the documentation, so it can be corrected.

That's for catching any character in the first /segment/. You can however get them with:
https://codeigniter4.github.io/userguide...i-segments

Nevermind... (: segment) are one segment. Will take a closer look at this.
Reply
#3

You need to catch it as an array. So it's working. :-)

PHP Code:
public function view(...$segments)
{
   
var_dump($segments);

Reply
#4

Thanks so much. I appreciate the assistance. 

For anyone else hoping to pull it down as a full string, the view function needs to be as follows:
PHP Code:
public function view(...$slug)
{
  $slug implode("/",$slug);
  dd($slug);


Reply
#5

There are a function for that, if you just wanted it as a string.
https://codeigniter4.github.io/userguide....html#path
Reply
#6

Doesn't work on me. It always return "Can't find a route for". I still can't figure it out.
Reply
#7

Put a / slash in front of it /(:any)
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB