路由器重定向到另一个页面

时间:2019-12-28 04:40:57

标签: laravel

我有类似的路线

Route::get('admin/selfcontacteditdata','SelfcontectController@edit')->name('selfcontectedit');
Route::post('admin/selfcontactupdatedata','SelfcontectController@update')->name('selfcontectupdate');

如果我只是转到浏览器并使用admin / selfcontacteditdata,它将重定向到

admin/newsshowdata

我的索引函数是

 public function __construct()
{
   return $this->middleware('auth');
}

public function index()
{

   request()->validate([
    'email' => 'required',
    'mobileno' => 'required',
    'facebook'=>'required',
     'google'=>'required',
      'map'=>'required',
    ]); 

    $data = selfcontect::find(1);
    return view('/admin/selfcontectedit',compact('data'));
}

我的中间件是

protected function redirectTo($request)
{
    if (! $request->expectsJson()) {
        return route('login');
    }
}

我的其余管理员路由运行正常。

4 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,但是我写的表名错误并且我的文件没有保存为.blade,请检查您是否也在做同样的事情,并且在编辑功能中没有验证的意义,您的编辑功能必须像< / p>

public class Bets
{
    public int Id { get; set; }
    public string name { get; set; }
    public string description { get; set; }
    public double team1 { get; set; }
    public double team2 { get; set; }
    public string sport { get; set; }
    public bool enabled { get; set; } // 1 = yes 2 = no
    public virtual User user { get; set; }
    public int? UserId { get; set; }
}

public class User
{
    public int UserId { get; set; }
    public string Username { get; set; }
    public byte[] PasswordHash { get; set; }
    public byte[] PasswordSalt { get; set; }
    public double moneyBalance { get; set; }
    public int admin { get; set; }
    public virtual IList<Bets> bets { get; set; }

}
public class BetForAddToUserDto
{
    public int Id { get; set; }
    public string name { get; set; }
    public string description { get; set; }
    public double team1 { get; set; }
    public double team2 { get; set; }
    public string sport { get; set; }
    public bool enabled { get; set; } // 1 = yes 2 = no
    public virtual User user { get; set; }
    public int? UserId { get; set; }
}

您的函数名称应为

答案 1 :(得分:1)

重定向到路线 如果在routes.php文件中有一个带有名称的路由,则可以将用户重定向到该特定路由,而无论其URL是什么:

app / Http / routes.php:

get('books', ['as' => 'books_list', 'uses' => 'BooksController@index']);

app / Http / Controllers / SomeController.php

 return redirect()->route('books');

如果将来要更改URL结构,这将非常有用–您只需要更改routes.php(例如,get('books',…to get('books_list',…),并且所有重定向将指向该路由,因此将自动更改。

如果有的话,您也可以使用路由参数:

app / Http / routes.php:

 get('book/{id}', ['as' => 'book_view', 'uses' => 'BooksController@show']);
 app/Http/Controllers/SomeController.php

return redirect()->route('book_view', 1);

如果有更多参数–您可以使用数组:

app / Http / routes.php:

 get('book/{category}/{id}', ['as' => 'book_view', 'uses' => 
 'BooksController@show']);

app / Http / Controllers / SomeController.php

  return redirect()->route('book_view', [513, 1]);

或者您可以指定参数名称:

  return redirect()->route('book_view', ['category'=>513, 'id'=>1]);

答案 2 :(得分:0)

enter image description here,您应该使用 Accept (接受)键而不是 Content / type(内容/类型)

答案 3 :(得分:0)

您不能通过视图重定向,实际上是您在调用视图。 正确的语法是

return view('view_name',compact('data'));

如果您想重定向到任何路线,都必须像这样呼叫

return redirect()->to('admin/selfcontacteditdata');