Home Too many Redirects with Controller
Reply: 0

Too many Redirects with Controller

user6231
1#
user6231 Published in May 23, 2018, 4:48 pm

When I try to define my routes with an ImagesController, I get a "site redirected you too many times" error when going to what should be the index. I'm stumped. Am I over-tired and not seeing something obvious?

web.php

Route::prefix('images')->group(function(){
    Route::post('add-tag', 'ImagesController@addTag');
    Route::get('test', 'ImagesController@index');
});

When I access /images/test, the controller responds correctly. When I have it defined as '' or / it redirects too much. I'm not setting them simultaneously.

Route::prefix('images')->group(function(){
    Route::post('add-tag', 'ImagesController@addTag');
    // like this:
    Route::get('', 'ImagesController@index');
    // or like the following: 
    Route::get('/', 'ImagesController@index');
});

I've even tried to define /images before the prefix group to no avail. images/test is fine in this case.

Route::get('images', 'ImagesController@index');
Route::prefix('images')->group(function(){
    Route::post('add-tag', 'ImagesController@addTag');
    Route::get('test', 'ImagesController@index');
});

ImagesController.php

namespace App\Http\Controllers;

use \Auth;
use App\Helpers;
use App\Image;
use App\Tag;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Symfony\Component\Console\Helper\Helper;

class ImagesController extends Controller
{
    private $sortBy = [
        'created_at',
        'updated_at',
        'orig_name',
        'orig_size'
    ];    
    private $defaultSortBy = 'created_at';
    private $defaultOrder = 'desc';

    /**
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        $sort = $request->get('sort', $this->defaultSortBy);
        $sort = in_array($sort, $this->sortBy) ? $sort : 'created_at';
        $order = $request->get('order', $this->defaultOrder);
        $order = $order == 'desc' ? 'desc' : 'asc';

        $images = Image::orderBy($sort, $order)
            ->whereQuestionable(0)
            ->wherePrivate(0)
            ->paginate(config('image.images_per_page'));

        if ($images !== null && $sort !== $this->defaultSortBy) {
            $images->appends(['sort' => $sort]);
        }

        if ($images !== null && $order !== $this->defaultOrder) {
            $images->appends(['order' => $order]);
        }

        return view(
            'images.index',
            [
                'images' => $images,
                'sort' => $sort,
                'order' => $order
            ]
        );
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    //this function works correctly, but I suppose it may be interfering?
    public function addTag(Request $request)
    {
        Log::debug(print_r($request->all(), true));

        $error = '';

        $uid = trim($request->input('uid', ''));
        $newTag = Helpers::prepTag($request->input('tag', ''));

        if (!Auth::check()){
            $error .= 'You can only add tags to images if you are logged in.';
        } else if ($uid === '' || $newTag === '') {
            $error .= 'UID and tag cannot be empty';
        } else {
            $image = Image::whereUid($uid)->unQuestionable()->first();
            $currentTags = [];

            if (count($image->tags)){
                foreach ($image->tags as $currentTag) {
                    $currentTags[$currentTag->id] = $currentTag->tag;
                }
            }

            if ($image === null) {
                $error .= "Image $uid was not found.";
            } else {
                $tag = Tag::firstOrNew(['tag'=>$newTag]);

                if ($tag->ip == '') {
                    $tag->ip = $request->getClientIp();
                    $tag->user_id = Auth::check() ? Auth::user()->id : 5;
                    $tag->save();
                }

                //\Log::info(print_r($currentTags, true));
                //\Log::info(print_r($tag, true));
                $addTag = !(bool)isset($currentTags[$tag->id]);

                $image->tags()->sync([$tag->id], false);
            }
        }

        if ($request->ajax()) {
            if ($error !== '') {
                return response()->json(['message' => $error],400);
            }

            return response()->json(['tagId' => $tag->id, 'imageUid' => $image->uid, 'tag' => $tag->tag, 'isAdmin' => true, 'addTag' => $addTag]);
        } else {
            if ($error !== '') {
                Helpers::alert(3, $error, 'Tag Not Added');
            }
            return redirect('/');
        }
    }
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.323998 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO