Using underscore synatx in URI with implicit controller in laravel - laravel-5

I know this:
If your controller action contains multiple words, you may access the action using "dash" syntax in the URI. For example, the following controller action on our UserController would respond to the users/admin-profile URI:
public function getAdminProfile() {}
Want to the know about:
If i want to use "underscore" synatx in the URI. Is it possible with implicit controllers.
Like want to use user/admin_profile in the URI. What would controller look like?

You can do something like this.
In your routes.php file add
Route::get('user/admin_profile', 'UserController#getAdminProfile');

Related

how to use two functions from same controller in single page route using get in laravel

Am trying to use two different functions from one controller in a single page route
Route::get('/cart','App\Http\Controllers\Frontend\CartController#index');
Route::get('/cart','App\Http\Controllers\Frontend\CartController#alldata');
But the problem is the function alldata works where the function index doesn't
You can't have 2 GET routes with the same path.
Route::get('/cart','App\Http\Controllers\Frontend\CartController#index');
Route::get('/cart/all','App\Http\Controllers\Frontend\CartController#alldata');
The /cart route is overwritten by the alldata(). So the alldata() is calling instead of index().
kindly remove the alldata()'s route and pass the data from index().
Route::get('/cart','App\Http\Controllers\Frontend\CartController#index');
Route::get('/cart','App\Http\Controllers\Frontend\CartController#alldata');
Try to manipulate your logic in controller rather than in route file.
Use conditional in controller function.

Product And Category Separation In Route (Laravel)

I'm setting up a new route system.
Route::get('/{cat1Url}', 'CategoryController#showCat1')->name('showCat1');
Route::get('/{productUrl}', 'ProductController#showProduct')->name('showProduct');
My sef link is after "/"
But,
{{ route('showProduct',[$p->pr_url]) }}
This method not working with route name. Working only upside route.
I don't want use
"/cat/myVariable"
or
"/product/myVariable"
Can't I use route name to work this way?
What is the solution to this?
In this way, if you make a get request to /something the laravel you start from top of web.php file looking to a route that follows the pattern. Your both routes will follow that pattern, but the laravel will always, pass the first one to controller.
You have two options:
Put only one route, and inside the controller you switch to the appropriate function. But this isn't a great ideia, because this is the function of the Web.php.
Use the routes like the documentation recommend:
Route::get('/cat/{catId}', 'CategoryController#showCat')->name('showCat');
Route::get('/prod/{productId}', 'ProductController#showProduct')->name('showProduct');
and in Controller you make the appropriate handler of your Category or Product.
You will have to have a way to tell Laravel which url to be mapped to what otherwise it will always use the last defined route. So in your case calling /myVariable and /myVariable it will use the latest definition which is showProduct. The only other way is if you use regular expression to differentiate the variables. For example:
Route::get('/{cat1Url}', 'CategoryController#showCat1')
->name('showCat1')->where('cat1Url', 'cat-*');
Route::get('/{productUrl}', 'ProductController#showProduct')
->name('showProduct')->where('productUrl', 'prod-*');
This way your slugs need to start with what you define, but you cannot use just id as a numeric value for both.

codeigniter with same route name with different controller functions

I want to keep routes like this
$route["signup"] = "Controller/signup";
$route["signup"] ="Controller2/fbsignup";
Is it possible to to give same route names with different controller functions.
Then how ?
No, you can't.
If you write same route again, it will overwrite first one.
It is just like an array variable.
If you assign any other value to variable, first one will overwrite.
But you can specify HTTP method with route.
No you can't do like this, you are going to make api then define method that will help
$route["signup"]["post"] = "Controller/signup";
$route["signup"]["GET"] ="Controller2/fbsignup";
I want to keep routes like this
$route["signup"] = "Controller/signup";
$route["signup"] ="Controller2/fbsignup";
then try this in routes and url should be like signup/signup and signup/fbsignup
$route["signup/signup"] = "Controller/signup";
$route["signup/fbsignup"] ="Controller2/fbsignup";
call the url like
signup/signup
signup/fbsignup

Codeigniter router get parameter

I use $.getJSON() to retrieve some data for a couple of cascading dropdowns in my form. $.getJSON() automatically appends the parameter at the end of the URL like domain.com/controller/method/?parent=5
So, I've declared my method like public function method($parent) which works file, but the same method will be used from other parts of the website that will call it like domain.com/controller/method/5
I tried to create a route in routes.php like the one below:
$route['business/regions/?parent=(:num)'] = 'business/regions/$1';
but it doesn't seem to work. Am I doing something wrong? Maybe ? is confusing the regex parser of the router? Do I have to escape it somehow to make it a 'literal' ? ?
Or is it that router is not used to 'rewrite' get parameters at all? I'm very confused, as it should work but it doesn't and I'm wondering what's wrong with it...
Codeigniter route parameters are for url parameters. It is particularly useful when trying to create a REST styled url pattern.
What you're trying to do is get url query string from the url which is not supported via the Codeigniter router. For you to get what you want you can do the following:
In your routes.php:
$route['business/regions'] = 'business/regions';
and in your controller Business.php:
public function regions() {
//the numeric id you're looking for
$parent = $this->input->get('parent');
}

MVC subController

My application has the following module:
/groups/{id_group} -- > GroupController.php
My question is this,
/groups/{id_group}/events/{id_event}
Which controller should handle this?
If you are using groups/n/ as routing, you would want to use your framework's routing system to route to the event controller. Otherwise, it is the groups controller.
If you want to stick to just using the first parameter as the Controller, then consider using the further parameters as purely parts of the query string e.g
?controller=groups&itemId={id_group}&action=events&actionId={id_event)
and use the controller to route it to the correct place e.g.
$model->getGroup($id_group, 'events', $id_event);

Resources