I have multiple routes which will directed to the same controller and method. I want the second route will have an empty customParams, but the first one will use the custom params. What should i do? Thanks
Route::get('{customParams?}/{slug}/{registrationCode}/detail', [SubmissionController::class, 'submissionDetail'])->name('submission.detail');
public function submissionDetail($customParams = '', $slug, $registrationCode)
{
//
}
First route was running perfectly
Detail
Second route did not work and produce 404 page
Detail
Make 2 routes for it, because in your case first, param is your optional.
in web.php
Route::get('{customParams?}/{slug}/{registrationCode}/detail', 'HomeController#details');
Route::get('{slug}/{registrationCode}/detail', 'HomeController#detail'); // add new route with different function name
in controller
/* your default function will call when your customParams having some value */
public function details($customParams = null, $slug, $registrationCode) {
dd($customParams, $slug, $registrationCode);
}
/* when your customParams will empty which is your 2nd case */
public function detail($slug, $registrationCode) {
$this->details(null, $slug, $registrationCode);
}
So as a result...
when URL calls with this http://127.0.0.1:8000/welcome/slug/r-code/detail
when URL calls with http://127.0.0.1:8000/slug/r-code/detail
Related
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.
Am sending a get request and adding the pagination value but now i would like to attach the page
So in my URL I have
http://localhost:8080/users?paginator=10&page=1
The value of page and paginator are controlled from frontend(angular4)
So in my controller I have
class UserController extends Controller{
public function index(){
return User::paginate($request->paginator);
}
}
The above works for the paginator value but now how do I attach the page number?
return User::paginate($request->paginator, ['*'], 'page', $request->page);
The default paginate method takes the following parameters.
public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null);
For further details, see this link.
From the docs here:
By default, the current page is detected by the value of the page query string argument on the HTTP request.
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'));
}
I want to pass multiple parameters from route to controller in laravel5.
ie,My route is ,
Route::get('quotations/pdf/{id}/{is_print}', 'QuotationController#generatePDF');
and My controller is,
public function generatePDF($id, $is_print = false) {
$data = array(
'invoice' => Invoice::findOrFail($id),
'company' => Company::firstOrFail()
);
$html = view('pdf_view.invoice', $data)->render();
if ($is_print) {
return $this->pdf->load($html)->show();
}
$this->pdf->filename($data['invoice']->invoice_number . ".pdf");
return $this->pdf->load($html)->download();
}
If user want to download PDF, the URL will be like this,
/invoices/pdf/26
If user want to print the PDF,the URL will be like this,
/invoices/pdf/26/print or /invoices/print/26
How it is possibly in laravel5?
First, the url in your route or in your example is invalid, in one place you use quotations and in the other invoices
Usually you don't want to duplicate urls to the same action but if you really need it, you need to create extra route:
Route::get('invoices/print/{id}', 'QuotationController#generatePDF2');
and add new method in your controller
public function generatePDF2($id) {
return $this->generatePDF($id, true);
}
I have a problem with this
I have un list of articles, and each element has a button to edit, how the next code:
<p>modifier l'article</p>
and I'm sending to the file route:
Route::get('/edit', 'ArticleController#edit');
to the file ArticleController method edit:
public function edit($idarticle)
{
$artic=article::find($idarticle);
if(is_null ($artic))
{
App::abort(404);
}
$form_data = array('route' => array('article.update', $artic->idarticle), 'method' => 'PATCH');
$action = 'modifier';
return View::make('article.create')->with('artic', $artic);
}
then I don't understand my error
Probably change Route::get('/edit', 'ArticleController#edit'); to Route::get('/edit/{idarticle}', 'ArticleController#edit');
Also
<p>modifier l'article</p>
needs to be
<p>modifier l'article</p>
The parameter in the router is not passed as an html parameter, but rather a part of the URL. So combines these two changes, it should be working.