Laravel 5 redirect () & # 8594; back () is not working properly

I have a problem with redirect()->back()when not HTTP_REFERER.

Usually when I open my URL with an identifier as a parameter to show a specific datarow: http://my-domain/module/ID

If the specified IDdoes not exist or the current user having loggedin does not have permission to open it, I do

if (!$this->checkPermission($id)) {
    return redirect()->back()->withError('message');
}
// do my stuff to show the datarow

But when I change the address field of the browsers and load the invalid one ID, then there is no HTTP_REFERERset and it redirect()->back()redirects me to my last loaded JavaScript for_this_view.js(and shows it), added to view the following:

app.blade.php

<!DOCTYPE html>
<html lang="de">
<head>
    @section('head')
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    {!! HTML::style('assets/jquery-ui/jquery-ui.min.css?ver=1.11.4') !!}
    <!-- some more scripts -->
    @show
</head>

<body>
    @yield('content')
</body>
</html>

show.blade.php

@extends('app')

@section('head')
    @parent
    {!! HTML::script('for_this_view.js') !!}
@stop

@section('content')
    <!-- display datarow -->
@stop

redirect()->back()? URL- , laravel ? ? , - .

.

UPDATE

, , ...

JavaScript, . , , JavaScript , Blade. /config/view.php , for_this_view_js.blade.php. for_this_view.js routes.php.

- :

/* load view javascripts */
/** @var \SplFileInfo $path */
foreach (new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator(public_path())
    ) as $path)
{
    if ($path->isFile()
        && ends_with($path->getFilename(), 'js.blade.php'))
    {
        Route::get('url_to_javascript.js', function() {
            $contents = View::make('for_this_view_js(.blade.php)');
            $response = Response::make($contents);
            $response->header('Content-Type', 'application/javascript');
            return $response;
        });
    }
}
+4
3

HTTP_REFERER , Laravel . URL- Illuminate\Session\Middleware\StartSession:

protected function storeCurrentUrl(Request $request, $session)
{
    if ($request->method() === 'GET' && $request->route() && ! $request->ajax())
    {
        $session->setPreviousUrl($request->fullUrl());
    }
}

, , , , JS:

  • GET
  • ajax

, .

:

app/Http/Kernel.php 'Illuminate\Session\Middleware\StartSession', $middleware. , no HTTP_REFERER , Laravel (/)

:

class CustomStartSession extends \Illuminate\Session\Middleware\StartSession {
    public function storeCurrentUrl(Request $request, $session){
        if(!ends_with($request->url(), '.js')){
            parent::storeCurrentUrl($request, $session);
        }
    }
}

, StartSession app/Http/Kernel.php , URL-, .js, " url"

Update

-, , . , register (, AppServiceProvider)

$this->app->singleton('App\Http\Middleware\CustomStartSession');
+3

, BaseController :

<?php namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesCommands;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;

abstract class Controller extends BaseController
{
    use DispatchesCommands, ValidatesRequests;

    /**
     * The fallback url for redirects...
     * Prevent faulty redirect if 'HTTP_REFERER' not set.
     *
     * @var string
     */
    private static $back_url = '';

    /**
     * @param string $back_url The fallback url for redirects...
     */
    public static function setBackUrl($back_url)
    {
        self::$back_url = $back_url;
    }

    /**
     * @return string
     */
    protected static function getBackUrl()
    {
        return self::$back_url;
    }

    /**
     * @return \Illuminate\Http\RedirectResponse
     */
    public static function redirect_back()
    {
        $back = self::getBackUrl();
        if (!empty($_SERVER['HTTP_REFERER']))
        {
            return redirect()->back();

        } else if (!empty($back)) {
            return redirect()->to($back);

        } else {
            return redirect()->to('/');
        }
    }
}

:

<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;

/**
 * Class MyController
 *
 * @package App\Http\Controllers
 */
class MyController extends Controller
{
    /**
     * Check permission for specified '$id'.
     *
     * @param int|string $id The datarow id
     * @return boolean
     */
    public function checkPermission($id)
    {...}

    /**
     * Display a listing of the resource.
     *
     * @param int|string $id The datarow id
     * @return Response
     */
    public function index($id)
    {
        if (!$this->checkPermission($id)) {
            self::setBackUrl(\URL::route('my_prefered_fallback_route'));
            return self::redirect_back()
                ->withErrors(["message"]);
        }

        return 'Hello, API';
    }
}
+1

, , storeCurrentUrl(), POST .

$request->setMethod('POST');
+1

All Articles