Same route on first link it works, but on second not. Laravel 5.6 - laravel

Route file web.php:
Route::get('/download/received/{image_id}/{isoriginal?}', 'DownloadController#download_recv_image');
View:
<li>Download {{strtoupper($image->extension)}}</li>
<li>Download PNG</li>
Function in controller:
public function download_recv_image($image_id, $original=false){...}
This is function for download received image. When I click on first link in view route is called and function is executed. But on second link where I'am not sending second parameter then it returns me error 404 and it looks like it cant catch route.
(I have another function for download user images, with same logic for route definition in another two links and there everything works.)

I have found where the problem is.
That's because above that route I have another route called:
Route::get('download/{image_id}/{isoriginal?}', 'DownloadController#download_user_image');
I have changed second route to /received/download instead of /download/received
It's messing up because both routes have the same beginning and parameters ar messed up.

Related

Set tab name in redirect to route in laravel

in laravel 8 i will redirect the user to a route after doing the query.
There are many tabs on the page where I want the user to be transferred
I wrote the code like this
(Of course I know it's wrong, but I wanted to try, and yet I did not think of another way
return redirect()->route('admin.auth.user.show', $user . "#edit-information")->withFlashSuccess(__('The user was successfully updated.'));
I get an error with this code
What is your solution?
What you can do to activate for example bootstrap tab based on #web paramater is use the redirect() method like this:
return redirect()->route('backend.settings.show', ['eshop' => $eshop->id, '#homeImages']);
Eshop is regular route parameter but '#homeImages' is added to the end of the route which is then picked up by the js on page load.
In Laravel there is a withFragment method, which add a fragment identifier to the URL.
return redirect()->route('admin.auth.user.show', ['user' => $user])->withFragment('edit-information');
https://laravel.com/api/8.x/Illuminate/Http/RedirectResponse.html#method_withFragment

Whats the difference between redirect and this in Codeigniter?

I am new in Codeigniter and it's one of the good frameworks of php. But on some conditions I'm confused. Like this one. If any of you have any clarification about my dough, it's a great help for me.
Offcouse redirects refresh the page and $this not but apart from this I want to know - anyhow both of them used to go to somewhere else on view pages or like in other controller or in same controller to other methods.
But we don't use these side by side because when getting any of them it will go to that page or method without checking the next lines.
In case of a normal difference then have lot's of but I just want to know about the condition of going to next page or method when we use redirect or $this like this -
$this->Function($value); //It's method of same controller.
redirect('Controller/function'); //It's also doing same with page reload.
Thank for looking my problem.
Redirect()
When you will call any function of helper in codeigniter then you can call function directly without using any object. Helper in Codeigniter is collection of functions.
Redirect() method is a part of URL helper in Codeigniter.
For your ref. https://www.codeigniter.com/user_guide/helpers/url_helper.html
So, just load helper using $this->load->helper('url'); or you can also mention in autoload.php file.
$this->Function(); used to call a function from same controller
$this->Function(); used to call a function from same controller
redirect()
While building a web application, we often need to redirect the user from one page to another page. CodeIgniter makes this job easy for us. The redirect() function is used for this purpose.
redirect($uri = '', $method = 'auto', $code = NULL)
The first argument can have two types of URI. We can pass full site URL or URI segments to the controller you want to direct.
The second optional parameter can have any of the three values from auto, location or refresh. The default is auto.
The third optional parameter is only available with location redirects and it allows you to send specific HTTP response code.
Redirect means jumping to another function mentioned in the redirect method.
$this->Function($value); => jumping to another function and you can execute the code of the same function as well as pass the value back by returning value.
When you send request to codeigniter generally CI controller gets called and then function which is mentioned in uri segment. like below... So this will be another request.
redirect('Controller/function'); //It's also doing same with page reload.
But when you have to call another function within the same request then you can use below approach
$this->Function($value); //It's method of same controller.
This will execute the given function and return the value within same request.

Laravel 5.7 Passing a value to a route in a controller

My controller posts a form to create a new page. After posting the form I need to redirect the user to the new page that will have the contents for that page that were entered in the previous form. If I simply do return view('mynewpageview', compact('mycontent')); where my mycontent is the object used to execute the $mycontent->save(); command, I carry the risk for someone refreshing the url thus posting the same content twice by creating a new page.
Instead I would like to redirect the user to the actual page url.
My route is
Route::get('/newpage/{id}', 'PageController#pagebyid'); and if I use return redirect()->route('/newpage/$pageid'); where $pageid = $mycontent->id; I get Route not defined error.
What would be the solution either to stop someone from resubmitting the content or a correct syntax for passing the parameter?
The correct answer that works for me is -
Give your route a name in the routes file
Then pass the parameters with an array as shown below in the controller.
return redirect()->route('newpageid', ['id' => $pageid]);
With basic (unnamed) routes, the correct syntax was return redirect('/newpage/'.$pageid);
You have already found out you can alternatively use named routes.
Last but not least, thanks for having considered the "double submit" issue! You have actually implemented the PRG pattern :)

laravel api route is not redirecting?

I am new to laravel,I had wrote api route code to register controller:
Route::post('test','Api\Auth\RegisterController#index');
In Register controller i had written simple code
public function index(Request $request)
{
return 'hello';
}
I am getting the output in postman like:
Sorry, the page you are looking for could not be found.
not hello.
Here the images:
1 3
Routes defined in the routes/api.php file are nested within a route
group by the RouteServiceProvider. Within this group, the /api URI
prefix is automatically applied so you do not need to manually apply
it to every route in the file.
You are trying to make a request to a route which does not exist.
In Postman
Change:
http://localhost:8080/App/api/test
To:
http://localhost:8080/api/test
You're telling laravel to route assoaciate '/api/test', not '/App/api/test', which is the adress you're trying to reach.
Also, if you plan to reach that address straight from the location bar of your browser, you should register the 'GET' method as well.
As you are returning something in function you need to use route as get() instead of post().
Route::get('test','Api\Auth\RegisterController#index');

Laravel Route::get() function and parameter confusing

Hello everyone I just installed laravel4 and spend two days trying to make the first step. Now I made it but I'm confused about the Route::get() function and his paremeters.
I installe laravel directly in
/opt/lampp/htdocs/laravel
then follow tutorial to create file
userform.php
into app/views, then add following codes into routes.php
Route::get('userform', function()
{
return View::make('userform');
});
. Then I go to
/localhost/laravel/public
to see welcome page, and
/localhost/laravel/public/userform
to see the form defined in the view/userform.php.
Q1: According to chrome dev tools, i see in the html page, the form action is
http://localhost/laravel/public/userform
but there is nothing under public but
index.php, favicon.ico packages robots.txt
Q2: for
Route::get('userform', function()
{
return View::make('userform');
});
what is the first "userform" represent?? according the official tutorial, it's supposed to be url, but what is the former part?
for this line
return View::make('userform')
I guess "userform" referes to the file /app/views/userform.php, right?
The .htaccess file in the public directory is responsible for funnelling all incoming requests through the index.php file. This allows Laravel to grab the URI and match it to the route you defined and eventually return to you the view you made.
So you request localhost/laravel/public/userform, the request is funnelled through index.php and Laravel is booted. Laravel picks off the userform part of the URI and matches it against your defined routes. It finds the route you defined and fires it and returns the response.
You're spot on with what you were thinking with your second question as well. When you call View::make the first argument is the name of the view you want to "make". If you named your view app/views/forms/user.php then you would return it like so in your route:
return View::make('forms.user');
Or you could use a slash:
return View::make('forms/user');

Resources