This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
Hello everyone, I'm having this issue where when I use the unique id for each row from my database to create a page for every id - I keep getting every page that I type, no matter what the url is (i.e /pagethatdoesntexist), to return my "Card" page without the database content because it doesn't match the id for any of the rows.
Am I using the "/:id" part wrong? Thanks for the information on this in advance!
title = "Card"
url = "/:id"
layout = "default"
is_hidden = 0
==
<?php
use RainLab\User\Models\User;
use System\Models\File;
function onInit() {
$id= $this->param('id');
$this['profile'] = Db::table('users')->where('is_activated', '=', '1')->where('id', $id)->first();
}
?>
==
Last updated
try adding validation to your page url like this:
url = "/:id|^[0-9]+$"
This will only match if the url contains an integer...
Last updated
Thanks for your reply. It kinda works but for example I have two users with IDs (1,2) and I am able to display data when I'm on the ID page (let's say "/1"). But I can write for example "/5642614672" and it will show me the page without any data on it because that ID doesn't exist. Is there any way to validate the page url to only IDs that exist? By the way, I'm using the "User" plugin and trying to display users profiles publicly for the context.
Last updated
In your onInit()
method, just do:
return $this->controller->run("404");
when the id does not match a user in the database.
Something like this:
try {
$profile = RainLab\User\Models\User::where('id', $id)->isActivated()->firstOrFail();
} catch (Illuminate\Database\Eloquent\ModelNotFoundException $e) {
return $this->controller->run("404");
}
Last updated
Thanks! This works perfectly. I'm beginner to Laravel and October so this helps me understand how things work. Hope you have a great day!
1-6 of 6