-
Sep 24th, 2024, 02:47 PM
#1
[RESOLVED] Node Style Routing in PHP
I wrote a web application using NodeJS and Postgres. When I purchased my hosting with Hostgator, I found out that they only offer PHP and MySQL under the plan I bought, so I'm in the process of rewriting it.
In my server.js file, I have this:
Code:
const express = require('express');
const footballRoutes = require('./football/controller');
const app = express();
const PORT = 3000;
// Use the imported routes for football and volleyball
app.use('/football', footballRoutes);
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
Then my football/controller.js, I have this:
Code:
const express = require('express');
const router = express.Router();
const footballService = require('./service');
router.get('/game/:id', async (req, res) => {
const id = req.params.id;
try {
const game = await footballService.getGame(id);
res.json(game);
} catch (err) {
res.status(500).send('Unable to get the football schedule.');
}
});
router.get('/roster/seasons', async (req, res) => {
const year = req.params.year;
try {
const roster = await footballService.getRosterSeasons(year);
res.json(roster);
} catch (err) {
res.status(500).send('Unable to get the football roster.');
}
});
router.get('/roster/:year', async (req, res) => {
const year = req.params.year;
try {
const roster = await footballService.getRoster(year);
res.json(roster);
} catch (err) {
res.status(500).send('Unable to get the football roster.');
}
});
router.get('/schedule/seasons', async (req, res) => {
try {
const schedule = await footballService.getScheduleSeasons();
res.json(schedule);
} catch (err) {
res.status(500).send('Unable to get the football schedule.');
}
});
router.get('/schedule/:year', async (req, res) => {
const year = req.params.year;
try {
const schedule = await footballService.getSchedule(year);
res.json(schedule);
} catch (err) {
res.status(500).send('Unable to get the football schedule.');
}
});
module.exports = router;
This allows me to make the following AJAX request (using Angular):
Code:
getFootballRosterBySeason(season: number): Observable<any[]> {
return this.httpClient.get<any[]>(`${environment.apiUrl}/football/roster/${season}`);
}
Essentially what this does is call a GET request but the NodeJS server knows to use the football controller's /roster/seasons endpoint without me having to pass the year as a query string parameter.
Is there a way that I can do this in PHP? I'm sure I could modify the .htaccess file but that'd get messy quick.
-
Oct 2nd, 2024, 10:47 AM
#2
Re: Node Style Routing in PHP
In the end I ended up changing my file structure to match the traditional PHP routing:
Code:
/football
/roster
-get.php
-seasons.php
/schedule
-game.php
-get.php
-seasons.php
Then changed my front-end code to match:
Code:
getFootballRosterSeasons(): Observable<number[]> {
return this.httpClient.get<number[]>(`${environment.apiUrl}/football/roster/seasons`);
}
getFootballRosterBySeason(season: number): Observable<any[]> {
const params = new HttpParams().set('season', season);
return this.httpClient.get<any[]>(`${environment.apiUrl}/football/roster/get`, { params });
}
getFootballScheduleSeasons(): Observable<number[]> {
return this.httpClient.get<number[]>(`${environment.apiUrl}/football/schedule/seasons`);
}
getFootballScheduleBySeason(season: number): Observable<any[]> {
const params = new HttpParams().set('season', season);
return this.httpClient.get<any[]>(`${environment.apiUrl}/football/schedule/get`, { params });
}
getFootballScheduleDetailById(id: number): Observable<any> {
const params = new HttpParams().set('id', id);
return this.httpClient.get<any>(`${environment.apiUrl}/football/schedule/game`, { params });
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|