What I'm trying to do is when users signup they have a custom url to their own page, like so:
www.mysite.com/username
How do I set the url manager in Yii to achieve this? I know you use this somehow.
<url:[a-zA-Z0-9_-]+>
Also with controller and action with that url:
www.mysite.com/username/controller/action
Put this AFTER ALL the other rules:
// one content type can have special urls
'<username>' => 'user/view',
Assuming your controller's function params are:
public function actionView($username) {
// code to get user by username (instead of by $id)
you can do what ever you want after reading this:
http://www.yiiframework.com/doc/guide/1.1/fr/topics.url#using-custom-url-rule-classes
i advice to read all article! it's helpfull!
Related
I have two question about generating routes in MVC 5.
This is example:
routes.MapRoute(
name: "ActionSite",
url: "{userName}/sites/{action}/{localSiteName}",
defaults: new { controller = "Site" }
);
#Url.RouteUrl("ActionSite", new { action="Edit", siteOrder = site.Order, localSiteName = site.LocalName, userName = ViewBag.UserName })
generates the next url:
https://localhost:44344/TestUser/sites/Edit/Site2?siteOrder=1
1)How to hide variables? I want to hide: ?siteOrder=1
2)TestUser is userName. At this moment I set manually it in all [Authorize] actions. Can I do this one time in some special method?
About first question, RouteUrl assumes you are going to use the URL in a GET method and when you are using GET verb you have to append query string to the URL. siteOrder has to be in query because you did not put it into URL template. Any extra parameter goes into query string.
Solution here is to use POST instead of GET. Down side would be losing simple GET calls ( anchor) from client side. You have to use #Html.BeginForm instead of #Html.ActionLink.
Second question is not clear, if you generate URL like https://localhost:44344/TestUser/sites/Edit/Site2?siteOrder=1 then you will get username form URL.
for example:
public ActionResult Edit(string userName, string localSiteName){ }
username here will be "TestUser" and you have to check authentication if userName is same as User.Identity.GetUserName()
Or you can write AuthenticationFilter to do the authentication job for you.
But if you mean a way that Url.RouteUrl automatically fill in userName property based on User.Identity I think answer is no. You have to retrieve username somewhere in ActionFilters or in Action or if you are using ASP.Identity then you already have the user name all the way down to the View, just you need to call
#User.Identity.GetUserName()
in your View or Controller to get it.
I'm trying to pass a list of URL's with Id attributes from a controller to a view.
I can pass a <a href=...> link back but I don't think writing a 'localhost' absolute path is a clean way of approaching this. I cant pass an ActionLink back as it returns the full string. Is ther a simple solution to this problem? Thanks in advance.
Using this overload of the UrlHelper.Action() method and Request object you can get a complete URL including the route parameters such as IDs and the actual hostname of the application.
string url = Url.Action("action", "controller",
new System.Web.Routing.RouteValueDictionary(new { id = id }),
"http", Request.Url.Host);
UrlHelper is available in the controller via its Url property.
You can then pass such URL into your view.
It is also possible to use UrlHelper directly inside your view to create URLs for controller actions. Depends if you really need to create them inside the controller.
Edit in response to comments:
Wherever you need to place the URLs, this "URL builder" you are looking for is still the UrlHelper. You just need to pass it (or the generated URLs) where you need it, being it inside the controller, view or custom helper.
To get the links inside the unsorted list HTML structure you mention, you need to put anchors inside the list items like this:
<ul>
<li>Link</li>
...
</ul>
Then again you just need to get the URLs from somewhere and that would be from UrlHelper.
Simple and easy.
text
the route id = the parameter that is going to be inserted into your method.
eg.
function Details(int id) {
//id has the value of my_var_id
}
I was creating a social networking site, in that each profile page needs to show using a seo friendly url.
Now its like www.siteprof.com/profile/23
i need to add aditioanl string 'jake-web-developer' at the end of this url. result will be like www.siteprof.com/profile/23/jake-web-developer. I know that this can be done when we create profile links. But if someone access this profile using www.siteprof.com/profile/23 this link, can i add that previous text to the url ?
Also is there any method to add these kind of particular string into url from controller actions ?
That doesn't look correct, by CakePHP convention, it should look like:
**www.siteprof.com/profiles/view/23**
After that you set up the profiles_controller to have the action view($id=0,$name="")
I don't know why that is important, but you can generate links with the name parameter and it would still work:
www.siteprof.com/profiles/view/23/jake-web-developer
If it's important to add the last string, then you should do a redirect in the view controller. Check if the $name is specified, and if it isn't, redirect to the URL with the name, although I'd advise against it since it's a performance hit.
My solution would be to put a custom redirect header in the page if the url doesn't contain the SEO friendly title which redirects to the full url
One method would be:
public function view($id = null, $slug = null) {
$data = $this->Profile->findById($id);
if ($slug == null) {
$this->redirect(array('action'=>$view, $id, $data['Profile']['slug']));
}
// .. rest of view logic
}
Hope this helps :-)
I'm using Symfony 1.4 and i want to forward in my controller to another controller and action with some parameters.
After creating a "bike" with "bike/create" i want to forward to "bike/show/id/X" with the id i got from my new bike instance.
$forwardString = 'bike/show/id/'.$bike->id;
$this->forward($url);
This does not work :-(
Maybe you can help! :)
Greetings!
The method definition for forward is public function forward($module, $action) the request should be preserved and if theese are things not currently in the request you will have to add them first.
Also you might even need redirect instead so the url changes and where you just give it the url public function redirect($url, $statusCode = 302) so usage would be $this->redirect('bike/show?id=' . $bike->id);
I have a form with a collection of checkbox's for a refine search function on my website.
I am trying to pass an array in a form GET but the URL looks like:
/search?filter=foo&filter=bar&filter=green
Is there a better way to pass this in MVC? Possible like
/search?filter=foo,bar,green
Thanks in advance.
There is now way you can change this URL. It is built by the browser.
You could change the URL by javascript before sending the request but the better way is to use the post redirect get pattern (PRG).
You first post to the controller and redirect to the url that you build in the controller. That way you have full controll over the URL.
EDIT
this is a sample
public ActionResult FilterResult()
{
RouteValueDictionary searchRoute = ControllerContext.RouteData.Values;
if (searchRoute["Filter"]==null)
{
searchRoute.Add("Filter","");
}
searchRoute["Filter"] = "filter1,filter2,filter3";
return RedirectToAction("Search", searchRoute);
}