Catch not found error by certain route prefix [Laravel]
<p>I'm building web api along side with website in laravel 5.2, when user visit unavailable url on my web like <code>http://stage.dev/unavailable/link</code> then it will throw on 404 page on errors view resource, then i want to catch with different way if user try to access my API with unavailable url like <code>http//stage.dev/api/v1/unavailable/link</code>, then i want to return <code>json/xml</code> response</p>

<pre><code>{
  'status' : 'not found',
  'code' : 404,
  'data' : []
}
</code></pre>

<p>not the view, is there a way to detect it by url prefix 'api/*' or how.., maybe another approach with similar result, so the device/client which access it could proceed by standard format (i have simple format in all json response)</p>

<pre><code>{
  'status' : 'success|failed|restrict|...',
  'api_id' : '...',
  'token' : '...',
  'data' : [
    {'...' : '...'},
    {'...' : '...'},
    ...
  ]
}
</code></pre>

<p><strong>SOLVED</strong></p>

<p>I figure out something after read answer from <a href="https://stackoverflow.com/users/614112/chris">Chris</a> and <a href="https://stackoverflow.com/users/1227923/alexey-mezenin">Alexey</a> this approach works for me i add couples of lines in <code>handler.php</code> at <code>render()</code> method,</p>

<pre><code>    if($e instanceof ModelNotFoundException || $this-&gt;isHttpException($e)) {
        if($request-&gt;segment(1) == 'api' || $request-&gt;ajax()){
            $e = new NotFoundHttpException($e-&gt;getMessage(), $e);
            $result = collect([
                'request_id' =&gt; uniqid(),
                'status' =&gt; $e-&gt;getStatusCode(),
                'timestamp' =&gt; Carbon::now(),
            ]);
            return response($result, $e-&gt;getStatusCode());
        }
    }
</code></pre>

<p>my header request respond 404 error code and return json data like i want..</p>