I have the following route:
reviews:
pattern: /reviews/{uid}{trailingSlash}
defaults: { _controller: unrealsp.cms.controller:handle_review, uid: "index", trailingSlash : "/" }
requirements: { trailingSlash : "[/]{0,1}" }
I made this work with trailing slashes using this stackoverflow answer.
The route will recognize the "/reviews" URL and refer to the index uid, displaying an index of available reviews (rather than single reviews it would call with any other uid). However, it will not recognize "/reviews/" as index, saying there is no such route. How do I go about changing that?
try changig it to:
pattern: /reviews/
{uid} makes the parameter required by the router, so if you want /reviews/ to be valid, you have to remove it from route definition. This way you would just add in the controller:
$uid = $request->get('uid','index');
Another solution is:
reviews:
pattern: /reviews/{uid}
defaults: { _controller: unrealsp.cms.controller:handle_review }
requirements: { uid: "\d+" }
reviews_index:
pattern: /reviews/
defaults: { _controller: unrealsp.cms.controller:handle_review }
Related
Currently i have the following route
routes.push({
path: "/",
redirect: "/Dashboard"
});
When I login, it will redirect me to the /dashboard screen.
But how can I update the route so that when I log in I can check the claims and then based on the role, redirect to a different view.
I tried the following, this is just sudo code as I cant really get it to work. Also if I can I would like to add an alias. Even if I can get it to work is would this be the correct way of doing it?
routes.push({
path: "/",
alias: "/dashboard", --> This does not seem to work with redirect.
redirect: to => {
getUserType(msalInstance).then(userType => {
if(userType == 'Client'){
return { path: '/clients/2/detail' }
}
else {
return { path: '/dashboard' }
}
});
}
});
and the route for the client-detail looks as follows.
routes.push({
path: "/clients/:id?/detail",
name: "client-detail",
props: true,
meta: {
requiresAuth: true,
layout: simpleLayout,
allowedUsers: clientUserTypes
},
component: loadView("client-detail"),
});
I have looked the the beforeEnter hook but when I add it, it seems to get ignored because the redirect is there.
Why I am getting errors for the route?
Other APIs for the CRUD operation are working fine.
I have added a new controller code for my new API and I am getting a route error.
What could be the mistake?
While I have already written my route in routing.yml
creat:
path: /create
defaults: { _controller: AcmeDemoBundle:DemoController:create }
methods: [GET]
Products:
path: /recentProducts/{id}
defaults: { _controller: AcmeDemoBundle:DemoController:recentProducts }
methods: [GET]
Update:
path: /updateProduct/{id}
defaults: { _controller: AcmeDemoBundle:DemoController:update }
methods: [POST]
Delete:
path: /deleteProduct/{id}
defaults: { _controller: AcmeDemoBundle:DemoController:delete }
methods: [DELETE]
referenceCreation:
path: /koco/create
defaults: { _controller: AcmeDemoBundle:DemoController:createReference }
methods: [GET]
My api is
http://localhost/koco1/web/app_dev.php/demo/koco/create
My Controller is
/**
* #Route("/koco/create", name="referenceCreation")
* #Template()
*/
public function createReferenceAction()
{
$organization = new Organization();
$organization->setName('Sensio Lab');
$user = new User();
$user->setName("Jonathan H. Wage");
$user->setOrganization($organization);
$dm = $this->get('doctrine_mongodb')->getManager();
$dm->persist($organization);
$dm->persist($user);
$dm->flush();
return new Response('Created product id ');
// $response = new Response('Hello Huzaifa ');
// return $response;
}
What is wrong here?
Kindly let me know.
Thanks
Your routing is off.
Also you are creating routes through yaml and through annotations. Should stick to one.
Also server setup looks off too, http://localhost/koco1/web/app_dev.php/demo/koco/create should be more like http://localhost/koco1/web/koco/create, you can try running php server. Going to /public and running php -S localhost:8000 you can read more here: https://www.php.net/manual/en/features.commandline.webserver.php
or symfony cli server https://symfony.com/download
Your routes does not match /demo/koco/create is not equal to /koco/create, you should first of all set up correctly, then it will be easier to debug.
I'm sure this is simple and I just haven't understood something, but could someone explain how I can redirect my myDomain.com/ or localhost:12345/ URLs to something like myDomain.com/lang/Controller/Index or localhost:12345/lang/Controller/Index?
My routing in Startup.cs looks like this:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "LocalizedDefault",
template: "{culture:culture}/{controller=Home}/{action=Index}/{id?}");
routes.MapRoute(
name: "default",
template: "{*catchall}",
defaults: new { controller = "Home", action = "RedirectToDefaultLanguage", culture = "en" });
});
For now, I have modified the launchSettings.json file to launch on the URL I want, but this type of solution isn't very flexible, and once I start using a server, I will be forced to redirect requests for myDomain.com anyway. Thanks in advance!
First modify routing definition:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "LocalizedDefault",
template: "{culture:regex(^[a-zA-Z]{{2}}(?:-[a-zA-Z]{{2}})*$)}/{controller=Home}/{action=Index}/{id?}"
);
routes.MapRoute(
name: "default",
template: "{controller}/{action}/{id?}",
defaults: new { controller = "Home", action = "RedirectToDefaultLanguage" }
);
});
You don't want a catchall route in order to avoid too many redirects. Please note I'm using regex constraint to make sure culture is in twoletters format (en, it, fr) or basic tagged names (en-US, it-IT, ...). If you want complete rfc4647 compliancy you need to change the regex in ^[A-Za-z]{1,8}(?:-[A-Za-z0-9]{1,8})*$, but I think it would be too much (unless you are planning to provide i-klingon website XD ).
Then the RedirectToDefaultLanguage action in HomeController:
public IActionResult RedirectToDefaultLanguage()
{
var currentCulture = Request.RouteValues["culture"];
var dest = Request.Path.Value == "/" ? "" : Request.Path.Value;
if (!string.IsNullOrWhiteSpace(Request.QueryString.Value))
dest += $"?{Request.QueryString}";
if (currentCulture == null)
{
return RedirectPermanent($"/en{dest}");
}
return RedirectToActionPermanent($"{currentCulture}{dest}");
}
Nevermind, this was a really dumb question. Just use the [Route("")] attribute on whatever controller action you want to get called on the default URL.
I want to make custom log \ history in order to track what happened. I have such Entities:
-Category
-Item
-AttrValue
-Attributes
All of them are related to each other like this:
I read it and decided to use EventListeners and write events to the table using doctrine. You do not need to offer to use bands for logging / auditing, the essence is what you need to do without them. Therefore, I determined such steps to realize this:
Create a new Entity with the following fields:
-Time (When);
-What (the action that occurred, for example, creating, updating, viewing or deleting records);
-OldValue (if it exists);
-NewValue (if it exists);
Create a new EventListener with prePersist, preUpdate and preRemove.
Create a method that will work with Doctrine and write values from item 1 (something like buildLog (LifecycleEventArgs $args, $type), where $type is an action (create, update, or delete).
Therefore, I have such questions:
1. Can I use EventListeners for what I want to do?
2. If so, did I correctly describe the sequence of actions? (to define EventListeners in services.yml I do not consider)
3. How to determine what action has occurred? (I will attach my routing file below)
4. How to use this all for several Entities? Something like this will suit?
$ entity = $ args-> getEntity ();
Can there be somewhere a similar implementation, so that you can read?
My Routing:
getAllCategory:
path: /category
methods: [GET]
defaults: { _controller: AppBundle:Catalog:getAllCategory}
getCategory:
path: /category/{id}
methods: [GET]
defaults: { _controller: AppBundle:Catalog:getCategory}
postCategory:
path: /category
methods: [POST]
defaults: { _controller: AppBundle:Catalog:postCategory}
updateCategory:
path: /category
methods: [PATCH]
defaults: { _controller: AppBundle:Catalog:updateCategory}
deleteCategory:
path: /category/{id}
methods: [DELETE]
defaults: { _controller: AppBundle:Catalog:deleteCategory}
getAllItem:
path: /items
methods: [GET]
defaults: { _controller: AppBundle:Item:getAllItems}
getItem:
path: /item/{id}
methods: [GET]
defaults: { _controller: AppBundle:Item:getItem}
postItem:
path: /item
methods: [POST]
defaults: { _controller: AppBundle:Item:postItem}
updateItem:
path: /item
methods: [PATCH]
defaults: { _controller: AppBundle:Item:updateItem}
deleteItem:
path: /item/{id}
methods: [DELETE]
defaults: { _controller: AppBundle:Item:deleteItem}
I got defined routes in routing.yml file
one route is:
Profile_user_profile:
path: /profile/{id}
defaults: { _controller: ProfileBundle:Users:profile }
methods: [get]
and second is:
Profile_accept_connection_proposal:
path: /profile/acceptProposal
defaults: { _controller:ProfileBundle:Users:acceptConnectionProposal }
methods: [put]
First route without methods: [get] listen also and [put] request and catch second url before it get to route definition. Is there way to define checking for parameter only if url is numeric.
Just add the requirements parameter to accept only digits for a determined route like this:
Profile_user_profile:
path: /profile/{id}
defaults: { _controller: ProfileBundle:Users:profile }
methods: [get]
requirements: <--- ADDED PARAMETER
id: \d+
For more infos read the Symfony book about Routing. There you can find more advanced example on how to use route parameters.
You can now do this with Annotations in your controller like so:
class UserController extends AbstractController
{
/**
* #Route("/profile/{id}", name="user_profile", requirements={"id"="\d+"})
*/
public function profile($id)
{
// ...
}
}
More info on Symfony's docs
Specifically defining routing requirements