How to route to a custom function in controller - laravel

I've create a function in UsersController named changepassword and also added following code to routes/web.php
Route::any("/users/changepassword","UsersController#changepassword")->name("users.changepassword");
But when I trying to access /users/changepassword go to function show rather than changepassword.

Related

Page redirection is not working from BaseController (Codeigniter 4)

I'm trying to make a separate function checkUser insede BaseController. But page redirection is not working inside this function. Even inside __construction() function this is not working also. But when I use it in initController or any other controller function, then this is working. How can I solve this problem, please guide me.
BaseController (In my case AdminBaseController)
This is Dashboard Controller
Showing this error
As you can see in the CI4 documentation, initController() is called after __construct(). Then, when you call checkUser(), your response object is still not initialized, thus null.
You could move the code of your __construct() method into the initController() method, in your AdminBaseController.

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');

call method before calling controller in every requests in laravel

I am using laravel 5 framework. I want to call a method that will call in every user request before calling controller method. How can I do this?
you can write a help function than call it in middleware handler function :
example:
add a new file app/Http/Helper.php with function somefunctionYouWillCall
in your compoer.json add "files": ["app/Services/Helpers.php"] to aotoload part,then composer dump-autoload.
in your middleware handle function call your function somefunctionYouWillCall(),return $next($request);
check laravel manual how to use middleware

Laravel add domains to controller routes

I have a Laravel App and a domain to access it. The Domain points to the public folder and executes the '/' route which then executes a certain method 'BaseController#index' in a controller.Now I want to add another domain. But this domain should execute another method 'AppController#run' (route can be '/app/run/.
How can I achieve this?
I created a vhost for this other domain that points directly to public/app/run. This works but the browser shows domain.com/app/run which I don't like.
So I think what I have do do is let this domain point to public and then in my routes file say that this domain shell execute 'AppController#run'
Or in the worst case it points to the '/' route and then inside the BaseController#index method I have to check what domain is accessing. But this seems not good to me.
Any ideas? I wonder why I can't find a lot on Google since this should not be only important to me.
First, all vhosts should have set the document root to the public public directory, otherwise Laravel won't bootstrap correctly.
Then you can add specific routes for that domain. For example:
Route::group(['domain' => 'seconddomain.com'], function(){
Route::get('/', 'AppController#run');
});
Now if you go to seconddomain.com run() in AppController will be called
Laravel Framework 5.7.2
I wanted a separate domain for my API.
My solution was to edit the mapApiRoutes() in Providers/RouteServiceProvider.php
protected function mapApiRoutes()
{
//This is what we're changing to Route:domain('www.sub.domain.com')
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}
and the function should look something like this:
protected function mapApiRoutes()
{
Route::domain('www.sub.domain.com')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}
You will also need to reconfigure your apache depending on what you're trying to accomplish.
Hope this helps.

How do I declare a controller method for a named route in Laravel 4?

I am attempting to declare a method/function in my controller that responds to a numerically named route. When I load any page in the site I receive an error stating the controller method could not be found, meaning that Laravel won't even load the application do to some incorrect formatting. I searched for an answer with no luck.
Here is the route I'm attempting to access via my Math controller:
students/academics/math/7-12
Here is the method declaration to look for the route:
public function get712()
Which gives me the following error no matter what page I'm loading:
Call to undefined method Illuminate\Routing\Router::get712()
I'm not sure how to name the function/method in my controller for a purely numeric routes since hyphen is not allowed and there is no upper/lowercase for numbers.
why not pass 7-12 as a variable to a method?
route:
students/academics/math/{number}
controller:
public function getMath($number)
{
// code here
}
I remembered that Laravel used to use underscores in the method name instead of camelCase so after scouring Google with no luck I declared the method like this:
public function get_7_12()
voila!

Resources