Redirect (301) one route to another from routing.yml in Symfony2 - redirect

I know this is probably trivial, but I couldn't find anything on the web or in the Symfony2 reference.
How do I redirect one route to another from routing.yml (with 301 status code)?
I'm looking for something like this:
SomeRoute:
pattern: /someroute
defaults: { _controller: SomeBundle:Controller:action }
AnotherRoute:
pattern: /anotherroute
defaults: { _redirect: {route: SomeRoute, status: 301} }
I could create a controller, but it seems overkill, since I don't have any parameters (and it would be overkill even so, if they are to be passed as they are).

SomeRoute:
pattern: /someroute
defaults:
_controller: SomeBundle:Controller:action
AnotherRoute:
pattern: /anotherroute
defaults:
_controller: FrameworkBundle:Redirect:redirect
route: SomeRoute
permanent: true # this is for 301
page: 5 # you can pass additional attributes

Related

Typo3 - ReDirect and RouteEnhancer

I am having trouble with my Extbase-Plugin, redirects and route enhancers:
I have a plugin that gets job offers via an API and displays as a list on a Typo3-page. I have written a route enhancer to make the URLs for the single job offers pretty.
If a user clicks on a link, the method "showJob" from the controller is called. This method redirects the user to a different page (I do need two distinct pages for this) and calls the method "showSingleJob".
I have written this redirect like this:
{
$currentPid = $GLOBALS['TSFE']->id;
$jobID = $this->request->getArgument('jobID');
$jobTitle = $this->request->getArgument('jobTitle');
$pageID = $this->request->getArgument('pageID');
$args = [
'jobID' => $jobID,
'jobTitle' => $jobTitle,
"pageID" => $pageID
];
$response = json_decode($this->curl_get_contents('https://api.jobsAPI.com/job/' . $jobID));
if ($pageID != $currentPid) {
$uriBuilder = $this->uriBuilder;
$uri = $uriBuilder
->reset()
->setTargetPageUid(intval($pageID))
->setAddQueryString(TRUE)
->uriFor('showSingleJob', $args, 'UmJobs', 'UmJobs', 'jobliste');
$this->redirectToUri($uri, 0, 404);
}
$this->view->assign('apiJob', $response);
}
The method "showSingleJob" simply gets the data for the desired job and displays it. This works just fine.
But I need pretty urls for the redirected page. At the moment I get my typo3-Url plus all the parameters I am passing. So I tried my hand at a route enhancer:
routeEnhancers:
UmJobs:
type: Extbase
limitToPages:
- 1
- 3
extension: UmJobs
plugin: jobliste
routes:
- routePath: '/karriere/'
_controller: 'UmJobs::list'
- routePath: '/karriere/{jobTitle}/{jobID}/{pageID}'
_controller: 'UmJobs::showJob'
_arguments:
jobTitle: jobTitle
jobID: jobID
pageID: pageID
defaultController: 'UmJobs::showJob'
- routePath: '/karriere/job/{jobTitle}/{jobID}/{pageID}'
_controller: 'UmJobs::showSingleJob'
_arguments:
jobTitle: jobTitle
jobID: jobID
pageID: pageID
defaultController: 'UmJobs::showSingleJob'
requirements:
jobID: '^[0-9].*$'
pageID: '^[0-9].*$'
jobTitle: '^[a-zA-Z0-9].*$'
Again this works fine for "showJobs" but It doens't work at all for "showSingleJobs". It seems as if the enhancer is completely ingored by $uribuilder.
Anyone have an idea what I am doing wrong?
Thanks a lot in advance!
Seems to me as if your URIs are not unique...
'UmJobs::showJob
routePath: '/karriere/{jobTitle}/{jobID}/{pageID}'
Now lets the values be:
jobTitle = job // Matching the requirement '^[a-zA-Z0-9].*$'
jobID = 123 // Matching the requirement '^[0-9].*$'
pageID = 987 // Matching the requirement '^[0-9].*$'
The path would be: /karriere/job/123/987
'UmJobs::showSingleJob
routePath: '/karriere/job/{jobTitle}/{jobID}/{pageID}'
Now lets the values be:
jobTitle = 123 // Matching the requirement '^[a-zA-Z0-9].*$'
jobID = 987 // Matching the requirement '^[0-9].*$'
pageID = '' // Matching the requirement '^[0-9].*$'
The path would be: /karriere/job/123/987
Conclusion
You can have the same URI for different actions - which can not be served.

TYPO3 9.5 Extbase How redirect a call of showAction with a invalide object to a custom page (not 404!)

I want to redirect a call of a showAction with an invalid uid to a custom page. How can I do this with TYPO3 9 when routing and a 404 handling is active? At moment I always end at the 404 because the page is not found..
Where should I attack?
checking plugin settings like throwPageNotFoundExceptionIfActionCantBeResolved? ignore param validation? overwrite errorAction or callActionMethod? write a custom global 404 handling? overwrite routing?
routing + error handling config:
...
type: Extbase
extension: ext
plugin: Pi1
routes:
-
routePath: '/{object}'
_controller: 'object::show'
_arguments:
object: object
defaultController: 'object::list'
defaults:
page: '0'
requirements:
object: '^[0-9].*$'
page: \d+
aspects:
object:
type: PersistedAliasMapper
tableName: tx_ext_domain_model_object
routeFieldName: uid
page:
type: StaticRangeMapper
start: '1'
end: '100'
...
errorHandling:
-
errorCode: '404'
errorHandler: Page
errorContentSource: 't3://page?uid=174'
Ok, the best way is to overwrite the TYPO3-Errorhandling..
config.yaml
errorHandling:
-
errorCode: '404'
errorHandler: PHP
errorPhpClassFQCN: My\secret\namespace\Error\ErrorHandling
ErrorHandling.php
namespace My\secret\namespace\Error;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Error\PageErrorHandler\PageErrorHandlerInterface;
use TYPO3\CMS\Core\Http\RedirectResponse;
class ErrorHandling implements PageErrorHandlerInterface{
/**
* #param ServerRequestInterface $request
* #param string $message
* #param array $reasons
* #return ResponseInterface
*/
public function handlePageError(ServerRequestInterface $request, string $message, array $reasons = []): ResponseInterface{
if (strpos($request->getRequestTarget(), '/page-where-i-want-my-special-404') !== false) {
return new RedirectResponse('/my-custom-404', 404);
}
return new RedirectResponse('/404', 404);
}
}
First of all, custom error handling is an easy way to solve the problem. Given solution by Kroff works fine.
But, it is just working in case of pages with one language. And it is not really useful to add a static path in the comparison (due to possibly changing page slugs).
Has anyone found a solution to check the called page id? Or even better add a default route directly in the route enhancers configuration?
By the way, if you want to keep the typed in url and still show the 404 page (default of typo3 page error handler) just change the return value return parent::handlePageError($request, $message, $reasons); and extend class by PageContentErrorHandler.

TYPO3-Slug not working with multiple arguments in f:link.action

I try to create speaking URL out of an action-link in TYPO3 9.5 to let the next page know from where the user came from.
When I use this code for the action-link:
<f:link.action pageUid="296" action="show" controller="Author" arguments="{author : blogentry.author, categoryParent : filtercategories.uid}"> {blogentry.author.firstname} {blogentry.author.surename}
</f:link.action>
and this in my config.yaml:
BlogAuthorPluginShow:
type: Extbase
extension: Blog
plugin: Blogauthors
routes:
- routePath: '/{author_title}'
_controller: 'Author::show'
_arguments:
'author_title': author
defaultController: 'Author::list'
aspects:
author_title:
type: PersistedAliasMapper
tableName: 'tx_ivsblog_domain_model_author'
routeFieldName: 'pathsegment'
it doesn't work because of the second argument (, categoryParent : filtercategories.uid). If I remove the second argument and write it like this it works:
<f:link.action pageUid="296" action="show" controller="Author" arguments="{author : blogentry.author}"
> {blogentry.author.firstname} {blogentry.author.surename}
</f:link.action>
How can I manage it to give the controller a second argument and make the slug work?
The argument categoryParent is not registered in config.yaml.
I'm not sure if it's enough to register it and use it there but i'd consider it as minimum requirement.

TYPO3 Ext. Custom Routes: Reverse Resolution not working

I am struggling with the URL generation of TYPO3 in my own extension.
Site Config:
routeEnhancers:
JobsPlugin:
type: Extbase
limitToPages: [11]
extension: Company
plugin: Jobs
routes:
- { routePath: '/{job_title}', _controller: 'Jobs::job', _arguments: {'job_title': 'id'} }
defaultController: 'Jobs::job'
requirements:
job_title: '[0-9]{1..6}'
aspects:
job_title:
type: PersistedAliasMapper
tableName: 'tx_company_domain_model_job'
routeFieldName: 'path_segment'
routeValuePrefix: '/'
Controller:
/**
* #param int $id
*/
public function jobAction(int $id) { }
Problem
The generated URL looks fine: /de/karriere/technischen-verkaufsberaterin-aussendienst
But when you want to access the page a PageNotFoundException is thrown.
Do I need to make any additional configurations or did I configure anything wrong?
Thanks in advance!
The problem was routeValuePrefix.
After I removed it, the URL could be resolved.
I assume, that this leads to a double slash when resolving: /de/karriere//technischen-verkaufsberaterin-aussendienst

pb with routing symfony2, some route generate 404

i have two routes, it works with one and it does not work with each other, really it's very strange :'(
route don't work and api generate service not found when i do /user
ard_api_user_get:
pattern: /user/{id}.{_format}
defaults: { _controller: BLAApiBundle:User:user, id: 0, _format: ~ }
requirements:
_method: POST
id: \d+
route work when i do /userr
ard_api_user_getr:
pattern: /userr/{id}.{_format}
defaults: { _controller: BLAApiBundle:User:user, id: 0, _format: ~ }
requirements:
_method: POST
id: \d+
any help please
the solution is to add a htaccess in the racine of the symfony project,
these is the content of the file :
Options -Multiviews -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /
DirectorySlash Off
# remove trailing slash
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]