I want to show different output - JSON or HTML.
I cannot use the \Request::ajax() feature as I just get normal request (JSON response is not based on XHR-Requests).
Is there perhaps a possibility to distinguish the output by different routes? E.g. check if the controller is called by a route with the prefix "mob" and then create a switch for the output based on that?
app/Http/routes.php:
Route::group( ['prefix' => 'api'], function( ) {
Route::resource( 'activation', 'ActivationController' );
//...
}
Route::group( ['prefix' => 'mob'], function( ) {
Route::resource( 'activation', 'ActivationController' );
//...
}
app/Http/Controllers/ActivationController:
<?php namespace App\Http\Controllers;
use App\Activation;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class ActivationController extends Controller {
public function index()
{
$activation = Activation::all( );
// switch to decide which output to show, based on the routes...
if ($prefix == "mob") {
return response()->view('template', compact($activation)); // not sure if it works this way
} else {
return response()->json($activation);
}
}
//...
I am open for pragmatic and easy solutions. Must not be the routes-solution, but a way where I do not have to change much code.
You can use the Request::is() method that accepts wildcards/placeholders. So you can do something like:
if($request->is('mob/*')) {
// do your mob response
} else {
// do the other response
}
If you have this everytime you do a response than you can write a custom response macro that handles the if and just take the data as an array and return this as json or give this to your blade view.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With