Below are the changes that need to be made to your existing code to implement the requested features. The changes are provided in git patch format.
1. Update the URL hash to open a specific profile and ensure the model doesn't change on page reload.
Files Modified:
app/app/Http/Controllers/ModelController.php
app/routes/api.php
app/resources/js/Pages/Models.vue
app/app/Http/Controllers/ModelController.php
diff --git a/app/app/Http/Controllers/ModelController.php b/app/app/Http/Controllers/ModelController.php
index e69de29..b3c3f45 100644
--- a/app/app/Http/Controllers/ModelController.php
+++ b/app/app/Http/Controllers/ModelController.php
@@ -0,0 +1,28 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use App\Models\Model; // Ensure you have the correct namespace for your Model
+use Illuminate\Http\Request;
+
+class ModelController extends Controller
+{
+ public function randomModel()
+ {
+ // Existing code to get a random model
+ $model = Model::inRandomOrder()->first();
+ return response()->json($model);
+ }
+
+ public function getModel($id)
+ {
+ $model = Model::find($id);
+ if ($model) {
+ return response()->json($model);
+ } else {
+ return response()->json(['message' => 'Model not found'], 404);
+ }
+ }
+}
+
+?>
app/routes/api.php
diff --git a/app/routes/api.php b/app/routes/api.php
index e69de29..f3e6a1b 100644
--- a/app/routes/api.php
+++ b/app/routes/api.php
@@ -0,0 +1,7 @@
+<?php
+
+use Illuminate\Support\Facades\Route;
+use App\Http\Controllers\ModelController;
+
+Route::get('/models', [ModelController::class, 'randomModel']);
+Route::get('/models/{id}', [ModelController::class, 'getModel']);
app/resources/js/Pages/Models.vue
diff --git a/app/resources/js/Pages/Models.vue b/app/resources/js/Pages/Models.vue
index d3b0a1e..c5e1f3b 100644
--- a/app/resources/js/Pages/Models.vue
+++ b/app/resources/js/Pages/Models.vue
@@ -1,6 +1,35 @@
<template>
<!-- Your existing template code -->
+ <div v-if="model">
+ <!-- Display model details -->
+ </div>
+ <div v-else>
+ <p>No more models available.</p>
+ </div>
</template>
<script>
+import axios from 'axios';
+
export default {
+ data() {
+ return {
+ model: null,
+ };
+ },
+ mounted() {
+ const id = window.location.hash.substr(1);
+ if (id) {
+ this.getModel(id);
+ } else {
+ this.getRandomModel();
+ }
+ },
+ methods: {
+ getModel(id) {
+ axios.get(`/api/models/${id}`).then(response => {
+ this.model = response.data;
+ window.location.hash = this.model.id;
+ });
+ },
+ getRandomModel() {
+ axios.get('/api/models').then(response => {
+ this.model = response.data;
+ window.location.hash = this.model.id;
+ });
+ },
+ },
}
</script>
2. Implement session handling to prevent showing the same profiles again and indicate when no more profiles are available.
Files Modified:
app/app/Http/Controllers/ModelController.php
app/resources/js/Pages/Models.vue
app/app/Http/Controllers/ModelController.php
diff --git a/app/app/Http/Controllers/ModelController.php b/app/app/Http/Controllers/ModelController.php
index b3c3f45..8d2e3a7 100644
--- a/app/app/Http/Controllers/ModelController.php
+++ b/app/app/Http/Controllers/ModelController.php
@@ -9,6 +9,30 @@ class ModelController extends Controller
{
// Existing code to get a random model
$shownModels = session()->get('shown_models', []);
+ $model = Model::whereNotIn('id', $shownModels)->inRandomOrder()->first();
+ if ($model) {
+ // Add current model ID to the session
+ $shownModels[] = $model->id;
+ session()->put('shown_models', $shownModels);
+ return response()->json($model);
+ } else {
+ // No more models to show
+ return response()->json(['message' => 'No more models'], 200);
+ }
+ }
+
+ public function getModel($id)
+ {
+ $model = Model::find($id);
+ if ($model) {
+ // Add current model ID to the session if not already present
+ $shownModels = session()->get('shown_models', []);
+ if (!in_array($model->id, $shownModels)) {
+ $shownModels[] = $model->id;
+ session()->put('shown_models', $shownModels);
+ }
+ return response()->json($model);
+ } else {
+ return response()->json(['message' => 'Model not found'], 404);
+ }
}
}
app/resources/js/Pages/Models.vue
diff --git a/app/resources/js/Pages/Models.vue b/app/resources/js/Pages/Models.vue
index c5e1f3b..d4f2e6c 100644
--- a/app/resources/js/Pages/Models.vue
+++ b/app/resources/js/Pages/Models.vue
@@ -17,16 +17,28 @@ export default {
},
getRandomModel() {
axios.get('/api/models').then(response => {
- this.model = response.data;
- window.location.hash = this.model.id;
+ if (response.data.message === 'No more models') {
+ this.model = null;
+ } else {
+ this.model = response.data;
+ window.location.hash = this.model.id;
+ }
});
},
getModel(id) {
axios.get(`/api/models/${id}`).then(response => {
- this.model = response.data;
- window.location.hash = this.model.id;
+ if (response.data.message === 'Model not found') {
+ this.model = null;
+ } else {
+ this.model = response.data;
+ window.location.hash = this.model.id;
+ }
});
},
+ },
+ watch: {
+ // Optionally watch for hash changes and load the corresponding model
+ '$route.hash'(newHash) {
+ const id = newHash.substr(1);
+ this.getModel(id);
+ },
},
}
</script>
3. Clean up the API response to remove unnecessary data and restrict direct access to the API.
Files Modified:
app/app/Http/Middleware/AjaxOnly.php (New file)
app/app/Http/Kernel.php
app/routes/api.php
app/Models/Model.php
app/app/Http/Middleware/AjaxOnly.php
diff --git a/app/app/Http/Middleware/AjaxOnly.php b/app/app/Http/Middleware/AjaxOnly.php
new file mode 100644
--- /dev/null
+++ b/app/app/Http/Middleware/AjaxOnly.php
@@ -0,0 +1,18 @@
+<?php
+
+namespace App\Http\Middleware;
+
+use Closure;
+
+class AjaxOnly
+{
+ /**
+ * Handle an incoming request.
+ *
+ * @param \Illuminate\Http\Request $request
+ * @param \Closure $next
+ * @return mixed
+ */
+ public function handle($request, Closure $next)
+ {
+ if(!$request->ajax()){
+ return response('Unauthorized.', 401);
+ }
+ return $next($request);
+ }
+}
app/app/Http/Kernel.php
diff --git a/app/app/Http/Kernel.php b/app/app/Http/Kernel.php
index a1b2c3d..d4e5f6a 100644
--- a/app/app/Http/Kernel.php
+++ b/app/app/Http/Kernel.php
@@ -29,6 +29,7 @@ class Kernel extends HttpKernel
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
+ 'ajax' => \App\Http\Middleware\AjaxOnly::class,
];
}
app/routes/api.php
diff --git a/app/routes/api.php b/app/routes/api.php
index f3e6a1b..a4b5c6d 100644
--- a/app/routes/api.php
+++ b/app/routes/api.php
@@ -3,5 +3,7 @@ use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ModelController;
-Route::get('/models', [ModelController::class, 'randomModel']);
-Route::get('/models/{id}', [ModelController::class, 'getModel']);
+Route::middleware('ajax')->group(function() {
+ Route::get('/models', [ModelController::class, 'randomModel']);
+ Route::get('/models/{id}', [ModelController::class, 'getModel']);
+});
app/Models/Model.php
diff --git a/app/Models/Model.php b/app/Models/Model.php
index e69de29..f0e1d2c 100644
--- a/app/Models/Model.php
+++ b/app/Models/Model.php
@@ -0,0 +1,14 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Model as EloquentModel;
+
+class Model extends EloquentModel
+{
+ // Specify the table if it's not the plural of the model name
+ // protected $table = 'models';
+
+ // Define which attributes should be visible in arrays
+ protected $visible = ['id', 'name', 'age', 'photo_url']; // Adjust fields as needed
+}
4. Optimize the application to achieve a PageSpeed score of 90+ on both mobile and desktop.
Files Modified:
webpack.mix.js
package.json
- Frontend assets (optimize images, minify CSS/JS)
webpack.mix.js
diff --git a/webpack.mix.js b/webpack.mix.js
index e69de29..a5b8c3d 100644
--- a/webpack.mix.js
+++ b/webpack.mix.js
@@ -1,5 +1,8 @@
const mix = require('laravel-mix');
mix.js('resources/js/app.js', 'public/js')
- .sass('resources/sass/app.scss', 'public/css');
+ .sass('resources/sass/app.scss', 'public/css')
+ .options({
+ purifyCss: true,
+ });
+if (mix.inProduction()) {
+ mix.version();
+}
package.json
diff --git a/package.json b/package.json
index a1b2c3d..d4e5f6a 100644
--- a/package.json
+++ b/package.json
@@ -5,7 +5,7 @@
"scripts": {
"dev": "npm run development",
"development": "mix",
- "watch": "mix watch",
+ "watch": "mix watch --hot",
"hot": "mix watch --hot",
"prod": "npm run production",
"production": "mix --production"
@@ -14,6 +14,7 @@
"devDependencies": {
"axios": "^0.19",
+ "laravel-mix-purgecss": "^5.0.0",
"cross-env": "^7.0",
"laravel-mix": "^6.0.6",
"lodash": "^4.17.19",
Additional Steps:
- Optimize Images: Compress images using tools like ImageOptim or TinyPNG before including them in your project.
- Lazy Load Images: Implement lazy loading for images to defer loading off-screen images until the user scrolls to them.
- Minify HTML: Use Laravel packages like laravel-html-minify to minify the HTML output.
- Enable Browser Caching: Configure your server to add caching headers to static assets.
- Reduce External Requests: Combine CSS and JS files where possible to reduce the number of HTTP requests.
- Defer/Async Scripts: Add
defer or async attributes to script tags where appropriate to prevent render-blocking.
By applying these changes to your codebase, you should achieve the requested functionality and performance improvements. Remember to test each change thoroughly to ensure that the application behaves as expected.