Override Laravel Routes with .htaccess - .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]

Related

Contentful: How to get an entry using nothing but one of its fields? Or, how to set the entryId in web app?

I am needing to make some sharable blog post URLs. To do that, the URL must be something like webpage.com/blog-post-title. It cannot be webpage.com/5GFd5GDSg2345WD.
Since I am using dynamic routing, I need to get a Contentful entry using nothing but what is on the URL. There should not be any queries because queries are ugly and reduce shareability, like webpage.com/blog-post-title?query=queriesAreUgly.
Unfortunately, I need the entryId to get the entry. Also unfortunately, the entryIds are all very ugly and therefore completely useless/unusable. I wish I could set my own entryId, but this does not appear to be possible for mysterious reasons.
I could make a lookup table that pairs URLs with entryIds, but I'm going to be handing this contentful project to someone who is not tech savy, and they should not have to manage a lookup table.
I could get all blog entries then filter by blog title, but, very obviously, this is inefficient, as I would be loading thousands of lines of text for no reason at all.
I could create my own backend API and doing all this myself, but this is also a bad solution because it would take too much time and I could not give it to my non-tech-savvy client.
There are seemingly no solutions to this problem which created by Contentful's inherent needless inflexibility.
The only efficient way to get this to work is to find the entry not by its ID but by one of its fields. Is there a performant/efficient way to do this, or am I just going to have to filter through every single blog post until I find the one with the correct title?
How about adding a 'slug' field to the blog post content type, which you can auto-generate from the title using the field settings (so you don't have to type it out manually?)
You can then filter on the slug field in the query.
If you're using the JavaScript SDK (which it sounds like you are), you can use getEntries() and filter by the slug field to get a single blog post. Like so:
import { createClient } from "contentful";
const client = createClient({
space: {SPACE_ID},
accessToken: {ACCESS_TOKEN},
});
const response = await client
.getEntries({
content_type: "blogPost",
limit: 1,
"fields.slug": "blog-post-title",
})
.then((entry) => entry)
.catch(console.error);

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.

How to pass param using query or route through a form?

form(action='/allusers', method="post")
input(id='name', placeholder='First Name / Last name')
button(type='submit') Launch Spacecraft
this is my html(jade) that redirects to allusers page from another page that takes user name as input but i cant pass the 'name' to allusers
router.post('/allusers', function(req, res, next) {
var name = req.query.name;
console.log(name);
res.render('allusers', { title: 'all users' });
});
Not to mention, i have already tired using req.param.name
but it only seems to work with req.body.name So i just want to know how to use the req.param and\or req.query because i cant change my URL from a form submit to extract information from it
Edit1: I kind of solve the issue which was in defining the form and it is suppose to be like input(type='',name='', placeholder='') and using req.body.name but still i dont know how to play with the URL yet
EDIT: I didn't noticed that you are sing id attribute instead of name which is the right one for defining form field names.
Id is only about identifying any (not only inputs) DOM elemnts in the DOM context. Field names should be specified by name attribute. This is why (as I figured out, but you didn't answered me yet), when you submit the form you don't see "name=xxxx" in the url.
Orignal (in this case wrong) answer):
In GET requests, form data (actual url parameters) come in req.query property (not req.params as I said before this edit, sorry).
Fixed example:
app.get('/user', function(req, res) {
res.send('user ' + req.query.name);
});
Anyway, see Express Request.query documentation.
Actually, according to http specification, url parameters can be provided even in POST, PUT, DELETE or PATCH requests. This is why they come in a different place: Because you always can have url parameters even when submitting your form thought POST, PUT, DELETE, PATCH (or any other specified in the future) request.

how to completely hide the url in yii2

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

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