I have url, localhost/user/about/id/5, i want it to convert to a like localhost/john.doe/about, is it possible to do it in Yii?
john.doe refers to username
about refers to action
i want to hide controller name, in this case user
Thanks for the help
Yes you'll need to use a custom UrlRule as in the docs here (Using Custom Url Classes). You can then strip apart the URL in your class, try and find a user name, if it doesn't exists simply return false and let the rest of the URL rules process.
Bear in mind the higher up the order of URL rules you place your custom one, the more often it will be run (as UrlManager will exit on the first matching rule) so it has performance implications if you just put it right at the top.
Bonus
This will also help you in generating URLs as you can just pass a user name as a parameter to a normal URL and have your class do the complicated bit.
Related
I am working on a webapp. There are items accessible the way using item id in the url web.site/123, where 123 is item id. I want to redirect visitors to the full url page like web.site/123-the-item, and I also want to use full links within my website.
Any good idea how to do it?
I don't need the code, I am looking for a simple yet nice and effective idea. The title is obviously in the database, so there will be one select needed anyhow. But I am not sure, if I should redirect once and every single time visitor visits the site without checking if the url is full (doesn't seem nice) or should I check the url and redirect only if it doesn't match the title?
I am using nginx, php, mysql and laravel 5.2 in case there is a workaround for this scenario I am not aware of.
Try adding route like
Route::get('/{id}',['uses' => 'ContentController#byId'],'where'=>['id'=>'\d*']);
And in that action you can check in database if id exists and redirect user to proper url
You can check use regular expressions to check the if the incoming request has just the id and have them mapped to particular routes. In the digit route you can redirect.
E.g.
your route file should be something like this :
Route::get('item/{id}', function ($id) {
//redirect to the full name route
})
->where('id', '[0-9]+');
Route::get('item', 'SomeController#somemethod');
Read more here.
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>'
I have written this rule in url manager so that it hide id during update action i want the url should be / the id should be hidden. But no luck.
eg:
I have this url mysite.com/controller/update/1 i want the url to be mysite.com/controller/update
What you are trying to achieve is impossible and useless.
Think about it logically. Action in this case simply won't know exactly what model should be loaded and updated.
I see only one possible use case of that.
When you load page post/update, then select post from list to update and load it with AJAX.
In this case just remove id from action parameters, you don't need to write additional url rules for that.
I'm completely lost on how to use the routing capabilities of Joomla for a custom component.
Here is an example link that is being sent to the custom component through a form submission:
http://superiordash.com/products/dash-kits/index.html?make_id=8&model_id=6&year_id=48&option=com_mmg&view=products&page=list&sel_make_id=-1&sel_model_id=-1&sel_year_id=-1&Itemid=580
What I want to do is grab the make_id, model_id, and year_id, find the corresponding values in the database and use them for the url as such: products/dash-kits/YEAR-MAKE-MODEL.html
So for the example link above:
http://superiordash.com/products/dash-kits/2002-acura-rsx.html
Any ideas from the greats out there?
Also, from what I understand from the documentation - Am I going to have to find a way to explode the rest of the query or it will be attached at the end of url? And if so - is there a way to explode every part of the query except year, make, and model, no matter what it is?
Any help would be most grateful. I've been searching for solutions, half solutions etc. and will continue to do so.
It's actually quite easy. You will need to use 2 functions in your router.php:
ComponentNameBuildRoute
and
ComponentNameParseRoute
The build route will return an array containing the elements to be displayed in the URL. For example, for a link that is http://www.test.com/products/category-name/product-name, the build route function will return array('category-name', 'product-name'); . The array is passed to a Joomla function automatically to generate the SEF URL.
The parse route function will translate the SEF URL into a GET URL. The function will receive $segments as a parameter, and that segments will consist of the portions in the URL, such as category-name and product-name. In that function, you will need to get the ID of the category and the product, and return an array that is something like array('category_id'=>$category_id, 'product_id'=>$product_id);
You can check the router.php for the banners component (which is the simplest component) and use it as a startup point.
I am a new Developer in Zend and I am working on a very simple application in Zend wich will allow me to publish the pages.
to do so, I need to create a way to get the controller name and action name if it does not exist.
e.g I have a controller named page and in the view action of that controller I display the page content based on the id I Pass. So he url will be:
http://localhost/page/view/slug/some-slug-value
I want that if I write the following link,
http://localhost/some-slug-value
instead of displaying Invalid controller error, it should also serch the db for that slug and open the view action in the page controller.
I don't want to change the URL http://localhost/some-slug-value even if i have to show some error.
If i had to process the invalid action, I can do it via the __call() function in that controller. so I need a __call function for handling invalid controllers.
As I am a new in Zend, please also mention where I have to write that code which you are going to suggest.
Thanks in advance
Zend_Controller_Plugin_ErrorHandler can handle the exception due to missing controller/action and is enabled by default. You can put your logic inside.
I have answered a similar question before using a custom plugin here.
Basically, it checks if a request can be dispatched and in the event it cannot, it redirects to another controller/action. This happens before the ErrorHandler plugin allowing for the ErrorHandler to handle errors only as intended.
EDIT
I should note that inn the plugin on the linked question, I referenced the username. In your case, you will have to substitute slug for username.
I'm looking to use mod_rewrite to mask user profiles on my site. However, their profiles are decided by their id in the format /profile.php?id=1. Both 'user' and 'id' are in the mySQL table 'users'. Is there a way to reroute the URL to read /user? Sorry if that's badly explained!
RewriteEngine on
RewriteRule ^profile/([^/\.]+)/?$ /profile.php?user=$1 [L]
This will cause all /profile/username to be redirected to profile.php?user=username. You can then check in your PHP:
if (isset($_GET['user'])) {
// Check if the username exists.
// SELECT ... FROM users WHERE username = ..
}
.htaccess will not be able to use the info from the database to do what you desire. Even if it could you shouldn't do it :).
But what you can do is to change the profile.php to be able to read an user by the name. Let's say to make profile.php able to process a new parameter name= and you add logic in it to look in the database either via an id or via the name. If you have this then is actually trivial to route / to /profile.php?name=.
You will also have to change the website generation to output proper urls.
Also look at Cristian's answer for more technical details. It's the same idea in the end.
You wouldnt change this in htaccess, or with any kind of config/rewrite rule. This would be done in your application PHP if I understand what you want.
in /profile.php?id=1 profile.php is (rewrite rules aside) the source PHP file, and id is a GET parameter. Its like an argument to profile.php when it executed. They come in the form source.php?get1=val1?get2=val2.... and you access them through the array $_GET
If you don't want users to see that id, you can do one of two things.
Replace it with user, as you suggest. URL would be 'profile.php?username=XXX'
Then in your code you will presumably need to perform a simple database query to get the id from the user name. Should be about 5 lines towards the top of the code in profile.php and the rest can remain unchanged Beware that this demands unique user names in case you werent before, and some special characters might be a problem with get parameters in the URL? For most user names, uniqueness is required and special characters are not allowed, so hopefully this isnt a problem.
Keep id but make it a POST peremeters. URL would be 'profile.php'
POST parameters are similar to get but they are not visible to the user because they aren part of the URL string. Just switch $_GET to $_POST in profile.php. The code will still identify users by id, but they won't be able to see their IDs.
In either situation you will need to change any incoming links.actions that lead to the descrubed URL that we are changing.
EDIT - Christian's has a good suggestion which is similar to #1 above but would allow for nicer looking URLs like profile/XXX rather than 'profile.php?username=XXX'. Code within profile.php would ne hte same, his changes are in profile.php are the same