Laravel Routing:: check parameter value and redirect - php

I have a route of GET type with some parameters. For example
Route::get('/my-route/{id}',array('uses'=>'myController#myAction'));
I want to check value of the parameter id and if this id=1 then redirect to another route else continue with it. What I am doing is like this
Route::get('/my-route/{id}',function($id){
if($id==1){
return Redirect::to(URL::route('my-another-route'));
}
else{
//What should I do here so my route works as before.
}
});
In else part I want my routes to myController#myAction along with parameters.
Thanks

You can do it as:
Route::get('/my-route/{id}',function($id){
if($id==1){
return Redirect::to(URL::route('my-another-route'));
}
else{
return app()->call(myController::class, ['id' => $id], 'myAction');
}
});

The easiest way to get this to work and route to your controller is put your Route back to the way it was and then the if statement to the top of your controller condition:
public function myAction() {
if ($id == 1) {
return Redirect::to(URL::route('my-another-route'));
}
to the top of your controller method.
Also, if you're only using uses => 'Controller#method' on with your route you can just do:
Route::get('/my-route/{id}','myController#myAction');
Hope this helps!

Related

Laravel 7 Binding Routes

I have this route declared on laravel:
Route::get('pages/{page}/{slug}', 'Common\Pages\CustomPageController#show')
->middleware(['web', 'prerenderIfCrawler']);
This route works fine and works if you make requests to:
https://example.com/pages/1/test-page
https://example.com/pages/2/other-page
https://example.com/pages/3/url-test
The problem is that I need a more friendly url as well as.
https://example.com/test-page
https://example.com/other-page
https://example.com/url-test
I want remove the suffix called pages, The numbers for the pages will never change and will be static for each one.
I've tried to make static routes for each one but can't get it to work.
Route::get('other-page', array('as' => 'other-page', function() {
return App::make('Common\Pages\CustomPageController')->show(2);
}))->middleware(['web', 'prerenderIfCrawler']);
I would appreciate a little help.
You could always get the URL segment in the Controller and use that to know what page you are on. If you don't want to do that you could pass extra information in the 'action' to specify the page:
Route::middleware(['web', 'prerenderIfCrawler'])->group(function () {
Route::get('test-page', [
'uses' => 'Common\Pages\CustomPageController#show',
'page' => 'test-page',
]);
...
});
Then you can get this extra information in the Controller:
public function show(Request $request)
{
$page = $request->route()->getAction('page');
...
}
If you knew all the pages you can use a route parameter with a regex constraint to restrict it to only those page names:
Route::get('{page:slug}', ...)->where('page', 'test-page|other-page|...');
public function show(Page $page)
{
...
}
You could just make use of a wildcard to catch your routes like this:
Route::get('/{slug}', 'Common\Pages\CustomPageController#show')
->middleware(['web', 'prerenderIfCrawler']);
Then in your controller:
public function show($slug)
{
$page = Page::where('slug', $slug)->first();
// ...
}
Just be careful with where you place the route. It should be at the end of your routes otherwise it will catch all the request of your app.
// ...
// my other routes
// ...
Route::get('/{slug}', ...);
By the way, if you want to bind your page models using the slug attribute do this:
Route::get('/{page:slug}', 'Common\Pages\CustomPageController#show')->//...
^^^^^^^^^
Then in your controller:
public function show(Page $page)
{ ^^^^^^^^^^
// ...
}
Check this section of the docs.

Laravel 5.4: controller method is called twice on a redirect to it

I'm encountering a problem where a redirect from one route to another is calling the targeted controller method twice. This question addresses a similar issue, but the OP passing a 301 status code was deemed to be the issue in the accepted answer, and I'm not specifying any status code. I'm also using the session state for parameters. The relevant code looks something like this:
public function origin(Request $request) {
// Assume I have set variables $user and $cvId
return redirect()
->action('SampleController#confirmUser')
->with([
'cvId' => $cvId,
'userId' => $user->id,
]);
}
public function confirmUser(Request $request) {
$cvId = session()->get('cvId');
$userId = session()->get('userId');
if (is_null($cvId) || is_null($userId)) {
// This is reached on the second time this is called, as
// the session variables aren't set the second time
return redirect('/home');
}
// We only see the view for fractions of a second before we are redirected home
return view('sample.confirmUser', compact('user', 'cvId'));
}
Any ideas what could be causing this? I don't have any next middleware or any of the other possible causes that are suggested in related questions where controllers are executed twice.
Thanks for any help!
Have you tried passing values in parameters? Try the below code.
public function origin(Request $request) {
// Assume I have set variables $user and $cvId
return redirect()->action(
'SampleController#confirmUser', ['cvId' => $cvId, 'userId'=>$user->id]
);
}
public function confirmUser(Request $request) {
$cvId = $request->cvId;
$userId = $request->userId;
if (is_null($cvId) || is_null($userId)) {
// This is reached on the second time this is called, as
// the session variables aren't set the second time
return redirect('/home');
}
// We only see the view for fractions of a second before we are redirected home
return view('sample.confirmUser', compact('user', 'cvId'));
}

How to make auth as user with route id in laravel

I would like to use auth in my login system with two roles:
admin
user.
When I redirect the route to user, it fails.
My Controller:
public function profile($id)
{
$santri = Santri::find($id);
return view('santri.profile', ['santri'=>$santri]);
}
My Route:
Route::group(['middleware' => ['auth', 'checkRole:admin,user']], function () {
Route::get('/santri/{id}/profile', 'SantriController#profile')->name('profiluser');
});
How I check the role:
{
$santri = Santri::all();
if(in_array($request->user()->role,$roles))
{
return $next($request);
}
return redirect()->route('profiluser', $santri);
}
Error:
Missing required parameters for [Route: profiluser] [URI: santri/{id}/profile].
The error message:
Missing required parameters for [Route: profiluser] [URI: santri/{id}/profile].
tells you that you are missing the parameter for this route: profiluser
As you can see here you do not call the route with the correct parameter, you are trying to pass the whole object instead of the id, so instead of this:
return redirect()->route('profiluser', $santri);
Do this:
return redirect()->route('profiluser', $santri->id);
But since you are already passing the whole object you could also do this, lets call it method B.
Here you want to find the model using the passed id:
public function profile($id)
{
$santri = Santri::find($id);
return view('santri.profile', ['santri'=>$santri]);
}
But since you already pass the whole object you could do this:
public function profile(Santri $santri)
{
return view('santri.profile', ['santri' => $santri]);
}
or this, which looks cleaner in my mind:
public function profile(Santri $santri)
{
return view('santri.profile', compact('santri'));
}
You need to pass $santry->id instead of just $santry. Change the line to:
return redirect()->route('profiluser', [$santri->id]);
According to your route:
Route::get('/santri/{id}/profile', 'SantriController#profile')->name('profiluser');
You must pass the user id, like this:
return redirect()->route('profiluser', ['id' => $request->user()->id]);

Passing parameter while redirecting to another route

I am trying to 'redirect to' from one controller method to another through the route. However, I want to pass some data as well. I tried Session::get('name') but doesn't seem to work. This is what I tried:
public function before() {
return Redirect::to('later')->with('x', 'y');
}
public function later() {
dd( Session::get('x') ); // null
dd( $x ) // not working
}
My route is like classic:
Route::get('/later', 'TheController#later')->middleware('auth');
What am I missing?
Instead of Session::get('x') try with session('x') as below.You can check for it using if (session()->has('x'))
public function later() {
dd( session('x'));
}

Laravel call route from controller

I am calling getting_started route after successfully login :
protected $redirectTo = '/getting_started';
Here is my getting_started route code :
Route::get('/getting_started','UserController#getting_started');
And controller code :
public function getting_started()
{
$id= Auth::id();
$user = DB::table('user_profiles')->where('user_id', '=', $id)->first();
if($user->dashboard_access == 0)
{
DB::table('user_profiles')
->where('user_id', $id)
->update(['dashboard_access' => 1]);
return view('user.getting_started');
}
return view('user.dashboard');
}
It works perfectly and show in url :
http://localhost:8080/getting_started
Now I actually want that if user.dashboard view is call it show in url like :
http://localhost:8080/dashboard`
And on getting_started view show :
http://localhost:8080/getting_started
It is possible to call dashboard route instead of :
return view('user.dashboard');
My dashobard route is :
Route::get('/dashboard',['middleware' => 'auth', function () {
return view('user.dashboard');
}]);
What I understand it is that you are looking for is this function
return redirect()->route('dashboard');
It's my understanding of your question which can be wrong. Maybe you are asking something else.
That called Redirection and especially you want to Returning A Redirect To A Named Route, you route called user.dashboard so you could redirect to it using redirect()->route(route_name) :
return redirect()->route('user.dashboard');
Hope this helps.

Categories