'<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.
Related
In codeigniter website i have url like this www.mywebsitename.com/used-motorbikes, In that page there is form to search location when user select location EX: location1 then the url should change to something like this
www.mywebsitename.com/used-motorbikes-location1 is it possible to add like this?
Add logic in client side js to form this URL first. Then redirect window location to this.
In codeigniter make sure you have route added for www.mywebsitename.com/used-motorbikes-* and handle it to give needed response.
$route['used-motorbikes-(:any)']
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.
I've built a webpage that, in its products page has this prototype for its url
http://www.example.com/products/show/some-really-nice-product/512
The last bit of the url is the product id, which serves the sole purpose of searching the db for the product.
But, is it possible, using routing, to be able to hide that part and still be possible to search the product by its id?
Like
http://www.example.com/products/show/some-really-nice-product/ ??
Thank you
If you don't want to put a number in your URL, you can't later search by a number/id :)
So you could add a VARCHAR field to the table, say "urlized", and use "some-really-nice-product" as its content for the given ID (you'll need to 'urlize' all your product names and make them unique). Don't forget to index your new field...
Then, you could access the product page using this URL which is very SEO friendly (ends with ".html"):
http://www.example.com/products/show/some-really-nice-product.html
...by adding a route to CodeIgniter, somehow like this:
$route['^products/show/(.*).html'] = 'products/show/$1';
In your controller/model, search the database for the string that is passed to "show" method, instead of the ID
if you hide id you can't retrieve that from the url anymore
if you want to hide that anyway just do:
$route['products/show/(:any)'] = "products/show/$1/$2";
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.
My site has articles and I would like the URL to be like so:
example.com/(article id)/(article title)
There are two reason I would like the URLs like so, SEO, and readability.
This setup is close to how stackoverflow is set up. And just like on stackoverflow, I don't want the title to be required but if it's not including in the I want the back-end to append the title to the URL.
To understand what I mean, take the current page's URL, remove the title form it and go to that new url. If you notice when the page is refreshed the title was automatically appended to the URL.
When I think about how to achieve this, it requires two queries. The first query will get the title based on the id, then form the new url, and redirect to it. The second URL will query the database with the id again this time displaying the article.
Is this the only way? Can I append the extra param without actually redirecting?
Side note: I am using cakePHP
I think that yes, this is the only way.
Call #1 to example.com/(article id): the server would query the database for the id, see that the url isn't the canonical one[1] and do a redirect.
Call #2 to example.com/(article id)/(article title) would behave as normal, meaning some more queries to the database...
^ I suggest you store an article's generated url path so you could use it in other places too.
This way, if no title was provided, the path would just be "/(article id)" and you wouldn't need to do a redirect here.