Is it possible to have custom urls without renaming controller methods when using RESTful routing with Route::controller?
With Route::controller('users', 'UsersController'); I want the url to be users/registration instead of users/create without renaming getCreate method in UsersController.
Yes you can. Before Route::controller('users', 'UsersController'); in your routes, you can add the following line:
Route::get('users/registration', 'UsersController#getCreate');
By doing that you're explicitly specifying that you want the GET request for users/registration to be handled by getCreate method on the UsersController class.
Related
So, for me the main 2 methods of adding middleware are in the routes files, api.php and web.php, or alternatively in the constructor of any Controller class.
However it occurred to me that there might be situations where I want to choose for a certain Request subclass to have some middleware applied to it. I haven't seen documentation for this. Is there a way to define middleware in these custom Requests?
For clarity, I'm referring to the Request classes that get made when you do php artisan make:request
I have a very small software and I use laravel and vue.js. I would like to know the difference between routing though api.php and web.php in routes folder. Can somebody explain me the difference in these two cases?
You can define the routes in either web.php or api.php. However, routes in api.php have certain advantages
routes get automatically prefixed with '/api/'
routes use api-middleware and auth. This can add certain additional security by throttling API-requests.
You should use api.php for all your api needs or any routes that will be called through ajax.
:
I am new to Laravel and by reading a bit its documentation, it seems that for each new page or url I need to define a route in the web.php file. Please correct me if I am wrong.
So, my question is... Is there a way to create a pattern as in YII framework or .NET framework to manage all routes (or most of them) at once by using something like:
{controller}/{action}/{id}
Well not exactly like Yii or .net but you can declare multiple routes in one line using Resource Controllers.
Laravel resource routing assigns the typical "CRUD" routes to a controller with a single line of code. For example, you may wish to create a controller that handles all HTTP requests for "photos" stored by your application.
You can do so by Route::resource('photos', 'PhotoController');
Read more here
How can I rewrite the following urls in CodeIgniter
localhost/users/show_profile/john
to
localhost/john
Thanks
you can achieve dynamic URL using CodeIgniter Routes.
Assuming
localhost/users/show_profile/john
We're looking at:
localhost/controller/method/variable
we can use the following route:
$route['([a-zA-z_]+)'] = "users/show_profile/$1";
You can access the variable by calling $this->uri->segment(1); inside show_profile() function.
IMPORTANT: keep the $route['([a-zA-z_]+)'] at the end of the routes.php file, to make sure it's not overriding any other routes.
If you only need to do it for a defined set of URLs, update /config/routes.php
If you need it to be dynamic then you should extend the CodeIgniter Router Library.
http://codeigniter.com/user_guide/general/creating_libraries.html (look for Extending Native Libraries)
In config/routes.php add $route['(:any)'] = "users/show_profile/$1";
More info on routes here
How to redirect controllers if they not match pattern.
For example I have follow controllers about_us, contacts, find_us. How to do so If the request method is not in that selection to be redirected to other controller ?
For some reason it's not in the documentation for URI Routing in the User Guide. If you are using CI2, inside of your routes.php file you can use "404_override". It is one of the reserved routes.
$route['404_override'] = "error";
So when a user comes to a controller that is not one you have created or not something inside your routes.php file, it throws them into the "error" controller. Obviously you would have to make a controller called "error"
Read about routing.