Skip to content

Commit

Permalink
Proof of concept : Uf powered API
Browse files Browse the repository at this point in the history
  • Loading branch information
lcharette committed Sep 11, 2023
1 parent b222d4b commit b0178e8
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 28 deletions.
4 changes: 4 additions & 0 deletions app/assets/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import axios from 'axios'
import VueAxios from 'vue-axios'

const app = createApp(App)
app.use(router)
app.use(VueAxios, axios)
app.provide('axios', app.config.globalProperties.axios) // provide 'axios'
app.mount('#app')
42 changes: 18 additions & 24 deletions app/assets/views/HomeView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,23 @@
export default {
data() {
return {
resources: [
{
url: 'https://getbootstrap.com/docs/5.3/getting-started/introduction/',
title: 'Bootstrap quick start guide'
},
{
url: 'https://getbootstrap.com/docs/5.3/getting-started/webpack/',
title: 'Bootstrap Webpack guide'
},
{
url: 'https://getbootstrap.com/docs/5.3/getting-started/parcel/',
title: 'Bootstrap Parcel guide'
},
{
url: 'https://getbootstrap.com/docs/5.3/getting-started/vite/',
title: 'Bootstrap Vite guide'
},
{
url: 'https://getbootstrap.com/docs/5.3/getting-started/contribute/',
title: 'Contributing to Bootstrap'
},
]
resources: [],
loading: false,
};
},
methods: {
getList() {
this.loading = true
this.axios.get('/api').then((response) => {
// console.log(response.data)
this.resources = response.data
this.loading = false
})
}
}
},
created() {
this.getList()
},
}
</script>

Expand Down Expand Up @@ -55,9 +48,10 @@ export default {
<h1 class="uk-heading-divider">Additional resources</h1>
<ul class="uk-list">
<li v-for="item in resources">
<a class="uk-button uk-button-default" :href="item.url">{{ item.title }}</a>
<a class="uk-button uk-button-default" :href="item.url">#{{ item.number }} - {{ item.title }}</a>
</li>
</ul>
<button class="uk-button uk-button-primary" @click="getList()" :disabled='loading'>Reload Resources</button>

<div style="padding-top: 25px; padding-bottom: 25px;"></div>

Expand Down
37 changes: 37 additions & 0 deletions app/src/Controller/AppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,41 @@ public function pageIndex(Response $response, Twig $view): Response
{
return $view->render($response, 'pages/index.html.twig');
}

public function api(Response $response): Response
{
$data = [
[
'url' => 'https://getbootstrap.com/docs/5.3/getting-started/introduction/',
'title' => 'Bootstrap quick start guide',
'number' => rand(1, 100)
],
[
'url' => 'https://getbootstrap.com/docs/5.3/getting-started/webpack/',
'title' => 'Bootstrap Webpack guide',
'number' => rand(1, 100)
],
[
'url' => 'https://getbootstrap.com/docs/5.3/getting-started/parcel/',
'title' => 'Bootstrap Parcel guide',
'number' => rand(1, 100)
],
[
'url' => 'https://getbootstrap.com/docs/5.3/getting-started/vite/',
'title' => 'Bootstrap Vite guide',
'number' => rand(1, 100)
],
[
'url' => 'https://getbootstrap.com/docs/5.3/getting-started/contribute/',
'title' => 'Contributing to Bootstrap',
'number' => rand(1, 100)
],
];

// Write empty response
$payload = json_encode($data, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT);
$response->getBody()->write($payload);

return $response->withHeader('Content-Type', 'application/json');
}
}
3 changes: 2 additions & 1 deletion app/src/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
namespace UserFrosting\Demo;

use Slim\App;
use UserFrosting\Routes\RouteDefinitionInterface;
use UserFrosting\Demo\Controller\AppController;
use UserFrosting\Routes\RouteDefinitionInterface;

class Routes implements RouteDefinitionInterface
{
public function register(App $app): void
{
// Use a catch-all route to allow the user to refresh the page when using vue-router createWebHistory API
$app->get('/api', [AppController::class, 'api'])->setName('api');
$app->get('/[{path:.*}]', [AppController::class, 'pageIndex'])->setName('index');
}
}
66 changes: 63 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"dependencies": {
"axios": "^1.5.0",
"uikit": "^3.16.26",
"vue": "^3.3.4",
"vue-axios": "^3.5.2",
"vue-router": "^4.2.4"
},
"devDependencies": {
Expand Down

0 comments on commit b0178e8

Please sign in to comment.