John Davidson

routes - how to use a filename different to the class name within the php file

0 comments
Message:


At first I didn't think I had a problem. I simply removed all hyphens from the path and selected the class that it represented. I suddenly realised that if you visited /forgot-password or /forgotpassword both would serve the page up, which is duplicate content, and obviously not how I want it to be. It's actually worse than that, the hyphen could be in any position and it would serve up the same page!


The only way that I have come up with so far is to create an array that maps out the path requested with hyphens to the class name without hyphens. So there is an added layer which checks the page requested against an object which contains the related classname. The thing is that I feel there must be a better solution and wondered if anyone here could assist me?


$routes[1] = "forgot-password";
$className = str_replace("-", "", $routes[1]);
$class = 'App\Models\\'.$className;
$this->pageObj->{$class} = new $class();

The above code serves the same page with or without the hyphen. Obviously the problem is that class names cannot have a hyphen in so the hyphens have to be removed but then I get the duplicate content problem. The other thing I have considered is to drop all hyphens out of the urls.



My Answer:
[
'forgot-password' => 'ForgotPassword',
'forgotpassword' => 'ForgotPassword',
'password-forgot' => 'ForgotPassword',
'passwordforgot' => 'ForgotPassword',
'reset-password' => 'ResetPassword',
'resetpassword' => 'ResetPassword',
'password-reset' => 'ResetPassword',
'passwordreset' => 'ResetPassword',
// Add more routes as needed
];

Rate this post

3 of 5 based on 6274 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us