how to completely hide the url in yii2 - .htaccess

Using yii2, i am creating a page to display the results of a category, and stuck in url rules.
Now, i have a category whose name is like training-centers and the id of this category is 5
and need to pass this categoryid to education controller with list action.
I need to write a rule of this in yii2,
'training-centers' => 'education/list' //how to pass the id
How to write this type of rule where id or other identifier is not avaialble in url?
Can/How we do it using .htacess?
After enabling the pretty url, it doesnt work with
RewriteRule training-centers index.php?r=education-list&id=5
In one line, how can we write the url in yii where controller/action/id are not identifiable in url?

Try this code:
'urlManager' => [
'rules' => [
'training-centers' => 'index.php?r=education-list&id=5'
]
],
Or in .htaccess
RewriteRule ^training-centers$ /education/list/?id=5

Related

Yii 2 URLs that contain text only, without ID

So like in wordpress, you can have URLs that are essentially
https://yourdomain.com/article-title/
Without any article id.
How do you do this with the Yii 2 platform? It seems like URL structures require an ID. Is there anyway to bypass this?
The best practice is :
http://example.com/<id>/<title>
because :
This will make more efficient from the speed perspective. When using ID in query.
When your db will be bigger and bigger in the future, Uses of the primary key ID is better for fetching data rather than a string field.
Search engines indexing the URL complete. So, users when searching in the search engines, it will fetch data LIKELY method, no EQUAL. (for example : google index > http://example.com/123/apple => [indexes: [123, apple, example.com, keywords, description, ...]], when you search 'apple', google will search in indexes that equal by 'apple', that your page have it and show on google results. So don't wory to indexing the your URL.)
In end :
if you are strong to do it, must be use slug field in your table and searchin by it.
add 'slug' field in your table
change add and edit actions in your admin module for automatically/manual to set slug field. (etc : I have an apple -> i-have-an-apple)
change URL creator to example.com/
I hope to help you.

Yii2 Adding SearchModel Many-to-Many relationships produces duplicative results

I have a model that has six many-to-many relationships. When I add the joinWith entries in the SearchModel and, then, look at what gets returned in the index.php view (with no search parameters applied), it shows many more rows than are actually in the base model. Upon further inspection, I find that multiple duplicative rows are being returned. It also throws pagination way off. For instance, if I add just one joinWith in the SearchModel and I've got pagination set to 10 rows per page, here's what happens. In the base model which has 175 rows, the first page will show "1-4 of 425 items". The second page will show "111-16 of 425 items." and the last item of the first page is duplicated at the top of the second page. To give some background:
The base model has the following relationships:
/**
* #return \yii\db\ActiveQuery
*/
public function getHerbalHerbsHerbalPreparations()
{
return $this->hasMany(\common\models\HerbalHerbsHerbalPreparations::className(),
['herbal_preparation_id' => 'id']);
}
/**
* #return \yii\db\ActiveQuery
*/
public function getHerbalHerbs()
{
return $this->hasMany(\common\models\HerbalHerbs::className(), ['id' => 'herbal_herb_id'])
->viaTable('herbal_herbs_herbal_preparations', ['herbal_preparation_id' => 'id']);
} */
In the SearchModel, I've got this entry:
$query = HerbalPreparations::find()->joinWith('herbalHerbs');
Please note that it makes no difference if I add the optional joinWith parameters for eager loading and join type. `$query = HerbalPreparations::find()->joinWith('herbalHerbs', true, 'LEFT JOIN'); produces the same results.
Without any joinWith, the index.php view looks like this:
With the joinWith described above, page 1 of index.php looks like this:
And page 2 of the index.php view looks like:
Note the duplication of the last row on page 1 and the first row of page 2.
Needless to say, this problem becomes exponentially worse if I specify the additional many-to-many relations with joinWith.
If I actually enter a search parameter using the GridView filter or a search form, it works perfectly. The problem is that the index.php view needs to work properly with no search parameter.
This seems like it would be a common problem for anyone using Yii2 in any kind of advanced application. I'm sure I must be doing something wrong, but I searched Google until I'm exhausted and haven't been able to find anything helps.
`
I can't take responsibility for this answer. Actually, Newbie on the Yii forum gave this answer. It's actually rather simple — just add a groupBy('id'). For instance
$query = HerbalPreparations::find()->joinWith('herbalHerbs', true, 'LEFT JOIN')->groupBy(['id']);
I suppose I should have been able to figure this one out from a basic knowledge of SQL, but I've never had to use groupBy in this way.
This seems like such a common need that I would think it would be documented and found just about everyplace on a simple search. Unfortunately, it doesn't appear to be.

Override Laravel Routes with .htaccess

Hey guys I've developed a school database grouped by city and state but I put cities word before routes.
so now every url comes like x.com/cities/city_name/state_name/school_name
my routes are like
Route::get('cities/{cityName}')
But now I want to delete this cities word because of SEO. When I remove cities word from my routes.php my other pages are don't work (admin, about_us)
so I thought maybe I can hide cities word with htaccess is it possible ? if so how can i do it ?
I know this won't be an answer you like, but take it more as a warning for future projects: use named routes.
If you had your route, say:
Route::get('cities/{cityName}', array('as' => 'city', 'uses' => 'CityController#city'));
And you had created your routes in the views using any of the provided methods:
route('city', array('cityName' => 'New York'));
form_open(array('route' => array('city', 'New York'));
URL::route('city', array('cityName' => 'New York'));
Now you wouldn't need to change anything in your views, you could just change the route itself:
Route::get('cities', array('as' => 'city', 'uses' => 'CityController#city'));
And forget about the rest (even the now missing parameter. I know, it won't look perfect...)
If you still want to go the .htaccess route you could try:
^cities/(.+)/(.+)/(.+)$ /cities/$2/$3 [L]

Query string in URL

So I've taken over a project a friend of mine had started but didn't have the time to finish. It's an online e-commerce system built with CodeIgniter. The problem is, every product's url is setup like site.com/store/viewProduct?id=3 with the ID as the identifier. Now I am okay with keeping the ID in the URL, but I would like to make it so the ? is removed and the product title is also added in as well. So the final URL would be like site.com/store/viewProduct/3/cake-cutter or something like that.
The controller functions all reference to $_GET['id']; as well. I know I will have to change that as well... but to what?
I tried changing the URLs to like shown above and then using $this->uri->segment(1,0); to get the product id, but I got an error.
I will use CodeIgniters built in url_title(); function to generate the product names, but what is the easiest way to go about changing these urls?
The best thing to do would just be to change the way the controller is getting your ID. I am assuming you don't need the 4th URI segment and that is just for SEO/usability purposes... but even if you do, you will still get it with the way this function is set up.
class Store extends CI_Controller {
//site.com/store/viewProduct/3/cake-cutter
public function viewProduct($id, $name)
{
//do stuff and now you have access to $id and $name
//$id == 3
//$name == 'cake-cutter'
}
}

CakePHP Search function with Paging Not Working

We are using CakePHP's default search behavior with listing page and once I have selected some criteria for searching, it works fine..
Now, whenever I go on page no. 2 with searched criteria, the search parameters does not pass with the Paging and it becomes a normal listing.
Do anyone have some idea about searching and paging combination, with CakePHP 1.2 default search plug-in.
Let me know your responses ASAP.
Thanks !
Take a look at this tutorial. It looks vaguely like what I've done in the past.
http://mrphp.com.au/code/search-forms-cakephp
If you work with Sessions, then the search criteria could be stored in the session.
You just need to make sure that the user also can easily reset the stored search criteria.
$this->paginate = array(
'conditions' => array('Model.name LIKE' => '%'.$storedInTheSession.'%'),
'order' => array('Model.name ASC'),
);
Assuming that your form is created with 'type'=>'get'
echo $form->create('Content', array('action' => '/index', 'class' => 'forms','type'=>'get'));
you can do something like:
if(isset($_GET['some_criteria'])){ //if some of you fields is set
unset($_GET['url']); //this is set by CakePHP and we don't need it
$paginator->options = array('url'=> array('controller' => 'content', 'action' => 'index', '?' => http_build_query($_GET)));
}
Although it might suit your needs I warn you that it might not be the CakePHPiest way

Resources