Laravel 5: build URL with custom parameters - php

I have an application in which you can choose between multiple customers.
Choosing a customer will generate following URL:
http://localhost:8000/customer/CUSTOMER_NAME
From there on, I would like to choose a specific sub-page (e.g.: the support page)
How do I generate the following link:
http://localhost:8000/customer/CUSTOMER_NAME/support
So far, I always lose my CUSTOMER_NAME parameter, and I do not know how to keep it.
The framework I use is Laravel 5.
Any ideas?

You shall do this by passing the url param to the view by
I believe you have something like this in your route
Route::get('customer/{id}', 'yourController#yourFunctionName');
Then, in your controller, you may have
public function yourFunctionName($id)
{
return view('yourViewName')->with('id', $id);
}
Then from your view can simply do this to generate a url like this
Click here
To have the url like below
http://yourprojectname/customer/18/support
Advice : Use the Primary key or any unique field rather than using name to avoid some future issues.
Also you shall use helpers to generate url's

Are you using named routes?
Route::get('customer/{name}', ['as' => 'customer.index', 'uses' => 'CustomerController#index']);
You could set up a new route like this:
Route::get('customer/{name}/support', ['as' => 'customer.support', 'uses' => 'CustomerController#support']);
with a method for the support section
public function support($name) {
return view('customer.support', [
'name' => $name,
]);
}
And in your layout you can link to the route.
Support

{{ route('pages.home', $array_of_custom_params) }}
This will generate the link:

Related

How would I define route to get me to a specific section on page in Laravel

I've a Register link on menu bar. On register page I have 2 forms, Login and Signup.
By default, it shows the Login form but I want it to show the Signup form first.
I thought If I do it with route by giving an Id to my signup form then I would be able to do that.
But it's giving me the error by saying
Missing required parameters for [Route: getRegister]
How would I modify my route?
Route
Route::get('/registration{url}', [
'uses' => 'niceActionController#getRegister',
'as' => 'getRegister'
]);
Controller
public function getRegister($url)
{
return view( '/' . $url, ['url' => $url]);
}
Header
{{ __('Register')}}
don't use the #url parameter in your web routes and controller, it is not meant for the backend.
Route::get('registration', ['niceActionController::class, 'getRegister'])->name('getRegister');
{{route('getRegister')}} will print out the link to the page, you'll want to put you parameter after the route, it will be handled after your page has loaded.
{{ __('Register')}}
in your controller you put the name of your blade template (not a route)
public function getRegister()
{
return view('nameofthetemplate');
}
Define your routes as below.
Route::get('action/{action_type}', 'NiceActionController#action') ->where('action_type', 'login|register') ->name("action");
In the view you can get route as below
{{route('action', ['action_type' => 'register'])}}
OR
{{route('action', ['action_type' => 'login'])}}
In the controller you can access the action_type parameter value as below.
$request->action_type;
Make sure you add Request $request parameter in method
Note:
The hash value is not accessible by the server script. You can either append action type in URL as shown in the above sample or include it in the query string. Check below link check how to get query string parameter value.
https://laravel.com/docs/8.x/requests#accessing-the-request
Tip: Please try to follow the Laravel naming conventions.
Because # symbol isn't parsed by the server, it's used by the browser for on-page navigation.
You might be familar with CSS/JS. # links in URLs will take you to either a named tag or ID tag in the page (e.g. http://page.com/#tag will take you to an element with the ID "tag")
When you hit http://page.com/registration/#register_forms the browser hit the page http://page.com/registration/ and searching for the id="register_forms" on your HTML.
Use this as your route
{{ __('Register')}}
Route::get('/registration/{url}', [
'uses' => 'niceActionController#getRegister',
'as' => 'getRegister'
]);
public function getRegister($url)
{
return view( viewNameHere, ['url' => $url]);
}

Laravel Route using array

Route::resource('posts','PostsController');
Route::post('posts/changeStatus', array('as' => 'changeStatus', 'uses' => 'PostsController#changeStatus'));
The code provided is a route from my laravel project. I did not write this code and I am attempting to understand what they have done. I cannot find anywhere in the documentation the reason for using the key value store with 'as' and 'uses'. I would normally write the code below, however this is not working with the ajax-crud setup.
Route::post('posts/changeStatus', 'PostsController#changeStatus');
From the docs:
Named routes allow you to conveniently generate URLs or redirects for a specific route. You may specify a name for a route using the as array key when defining the route
as is the name of that route. You can use it to create an URL with route('changeStatus') helper.
uses is controller method (action) for the route.
https://laravel.com/docs/5.1/routing#named-routes

Alias for a route with a fixed parameter value

I have this route:
Route::get('/MyModel/{id}', 'MyController#show');
The method show() accepts a parameter called id and I want to setup an alias for /MyModel/1 so it's accesible from /MyCustomURL.
I already tried a few combinations, like:
Route::get('/MyCustomURL', ['uses' => 'MyController#show', 'id' => 1]);
But I keep getting missing required argument error for method show().
Is there a clean way to achieve this in Laravel?
In Laravel 5.4 (or maybe earlier) you can use defaults function in your routes file.
Here is example:
Route::get('/alias', 'MyController#show')->defaults('id', 1);
In this case you don't need to add additional method in your controller.
In same controller (in your case MyController ?) you should create one new method:
public function showAliased()
{
return $this->show(1);
}
and now you can define your aliased route like so:
Route::get('/MyCustomURL', 'MyController#showAliased');
define your route like this:
you can use "as" to give your route any name that you need.
Route::get('/MyModel/{id}' , [
'as'=>'Camilo.model.show',
'uses' => 'MyController#show' ,
]) ;
now if you want to access this route, you can generate url for it, based on its name like this:
route('Camilo.model.show', ['id' =>1]) ;
Route::get('MyModel/{id}', 'MyController#show');
not
Route::get('/MyModel/{id}', 'MyController#show');
Good Luck!

How to do a simple redirect in Laravel?

I have a function in Laravel. At the end I want to redirect to another function. How do I do that in Laravel?
I tried something like:
return redirect()->route('listofclubs');
It doesn't work.
The route for "listofclubs" is:
Route::get("listofclubs","Clubs#listofclubs");
If you want to use the route path you need to use the to method:
return redirect()->to('listofclubs');
If you want to use the route method you need to pass a route name, which means you need to add a name to the route definition. So if modify your route to have a name like so:
// The `as` attribute defines the route name
Route::get('listofclubs', ['as' => 'listofclubs', 'uses' => 'Clubs#listofclubs']);
Then you can use:
return redirect()->route('listofclubs');
You can read more about named routes in the Laravel HTTP Routing Documentation and more about redirects in the Redirector class API Documentation where you can see the available methods and what parameters each of them accepts.
Simply name your route:
Route::get('listofclubs',[
'uses' => 'Clubs#listofclubs',
'as' => 'listofclubs'
]);
Then later
return redirect()->route('listofclubs');
One method is to follow the solution provided by others. The other method would be, since you intend to call the function directly then you can use
return redirect()->action('Clubs#listofclubs');
Then in route file
Route::get("listofclubs","Clubs#listofclubs");
Laravel will automatically redirect to /listofclubs.

Laravel 5 redirect to path with parameters (not route name)

I've been reading everywhere but couldn't find a way to redirect and include parameters in the redirection.
This method is for flash messages only so I can't use this.
return redirect('user/login')->with('message', 'Login Failed');
This method is only for routes with aliases my routes.php doesn't currently use an alias.
return redirect()->route('profile', [1]);
Question 1
Is there a way to use the path without defining the route aliases?
return redirect('schools/edit', compact($id));
When I use this approach I get this error
InvalidArgumentException with message 'The HTTP status code "0" is not valid.'
I have this under my routes:
Route::get('schools/edit/{id}', 'SchoolController#edit');
Edit
Based on the documentation the 2nd parameter is used for http status code which is why I'm getting the error above. I thought it worked like the URL facade wherein URL::to('schools/edit', [$school->id]) works fine.
Question 2
What is the best way to approach this (without using route aliases)? Should I redirect to Controller action instead? Personally I don't like this approach seems too long for me.
I also don't like using aliases because I've already used paths in my entire application and I'm concerned it might affect the existing paths if I add an alias? No?
redirect("schools/edit/$id");
or (if you prefer)
redirect("schools/edit/{$id}");
Just build the path needed.
'Naming' routes isn't going to change any URI's. It will allow you to internally reference a route via its name as opposed to having to use paths everywhere.
Did you watch the class Illuminate\Routing\Redirector?
You can use:
public function route($route, $parameters = [], $status = 302, $headers = [])
It depends on the route you created. If you create in your app\Http\Routes.php like this:
get('schools/edit/{id}', 'SchoolController#edit');
then you can create the route by:
redirect()->action('SchoolController#edit', compact('id'));
If you want to use the route() method you need to name your route:
get('schools/edit/{id}', ['as' => 'schools.edit', 'uses' => 'SchoolController#edit']);
// based on CRUD it would be:
get('schools/{id}/edit', ['as' => 'schools.edit', 'uses' => 'SchoolController#edit']);
This is pretty basic.
PS. If your schools controller is a resource (CRUD) based you can create a resource() and it will create the basic routes:
Route::resource('schools', 'SchoolController');
// or
$router->resource('schools', 'SchoolController');
PS. Don't forget to watch in artisan the routes you created

Categories