Is there a tidy way to implement the SS lesson regarding DataObjects as pages to include a SEO friendly URL rather than a page ID. I have done this before in version 3.x but as of 4.x it not longer seems to work.
It seems easy enough to generate the link on the DataObjects by changing
public function Link()
{
return $this->RegionsPage()->Link('show/'.$this->ID);
}
to
public function Link()
{
return $this->RegionsPage()->Link('show/'.$this->Title); //or URLSegment or another field
}
but how would I modify the controller to accept the URLSegment or Title or whatever
public function show(HTTPRequest $request)
{
$region = Region::get()->byID($request->param('ID'));
if(!$region) {
return $this->httpError(404,'That region could not be found');
}
return [
'Region' => $region,
'Title' => $region->Title,
];
}
I don’t know why you want to pass the title and not the unique ID. Such a title can exist multiple times, but not an ID!
But if you want to test it, you can try the following:
In the show function you can of course not search for the ID in the region if you transfer the title.
Your SQL query currently looks something like this:
SELECT * from Region WHERE ID = "<Title>";
It cannot work.
It should look something like this: (untested!)
$region=Region::get()->filter (['Title' => '$ request-> param (' ID ')');
However, you now have the problem that you can also get more like a data set. Try that with an additional "->first()", then you will always get the first record.
There exists this module I wrote to do exactly what you’re asking for
It basically does what everyone has described above, but in a simple to apply manner.
The readme should hopefully explain the usage in an easy to understand manner, but I always welcome feedback if you have trouble!
It was originally written in as a response to a very similar question in the past, which I then ‘modularised’. Recently I updated it to be Silverstripe CMS 4 compatible, but it should be well documented in the code to continue helping people wanting to learn about this functionality as you are.
It does have a few extra features though such as avoiding the conflict problem RALF described above.
Great you managed to solve the first problem!
The second issue you’re having appears to be because you don’t have any slug_trails defined on the Controller or DataObject being slugged.
You need to either define this config setting, or pass a parameter to the extension application similarly to how Slug does - e.g. SlugHandler::class . "('MyItems')"
Hopefully the configuration section of the readme can help - although I realise the examples are missing this configuration option now looking at them again