Change the URL for each element in the database


#1

Hi everyone,

I will explain as best I can, I’m sorry :slight_smile:

well, I have a page in my kirby that gets information of de database:

In the home page each form goes to /specimen but the information is sent to the controller is different (I pass the id of an element in the database to take all the information after).

So, I want the url in each option to be /specimen/name (name is another field of the database)

How can I do this? Thank you!


#2

I’m not 100% sure if this works via form submit endpoints, but have you had a look at routes already?


#3

Hi, thank you for your answer.

I’ve tried this code:

c::set('routes', array(
  array(
    'pattern' =>  'specimen/(:all)',
    'action' => function($font){
      $page = page('specimen/' . $font);
      go($page);
    }
  )
));

That doesn’t run, when I go to specimen/something appears the error page.


#4

Maybe you can provide more information about your setup, I didn’t really understand your first post? You have a database, and you have forms. and pages … How does all that relate to each other?


#5

Well, I will explain better, sorry :sweat_smile:

In my database I have a list of products and I print them into the home page. Each element has a form with a hidden field with the row id value. This value is sent to /specimen page which has a controller to take all the information about the product. I have to customize the url as /specimen/name-product.

I don’t know if it’s possible to do it with routes.

Thank you for your time :slight_smile:


#6

Are we talking about action attribute URL here? Or what is the purpose of that URL?


#7

The purpose is to know where you are. If you are in /specimen of the product /a or in /specimen of the product /b


#8

It is only a visual purpose. I don’t do anything with that value. I only want to see where I am


#9

So specimen is a single product page that you are referred to if you click on a product link? If so, then. routes are the way to go. You return the specimen page with additional information for the template, see the docs:

https://getkirby.com/docs/developer-guide/advanced/routing
https://getkirby.com/docs/developer-guide/advanced/controllers#arguments-from-the-router


c::set('routes', array(
  array(
    'pattern' =>  'specimen/(:any)',
    'action' => function($any){
      $data = [];

      return ['specimen', $data]; // return the specimen page with additional data, not a non. existing page
    }
  )
));

Note that you return the specimen page here, but with information for the particular product page.
You fetch the data in your controller using the fourth argument.

If you don’t need additional data, then you can leave that part out.


#10

Thanks a lot, It was what I needed! :slight_smile: