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
Related
I want to get the session id of the user each time a page is loaded so I use \Request::getSession()->getId() in the controller construct but I get this error
Call to a member function getId() on null
If I use same code in any of the route methods, it works.
How can I solve this?
Try use Illuminate\Support\Facades\Auth; Auth::getSession()->getId();
This is the default functionality of Laravel. Session data is no longer available in the constructor. See:
laravel - Can't get session in controller constructor
Taylor's word about it :
It’s very bad to use session or auth in your constructor as no request has happened yet and session and auth are INHERENTLY tied to an HTTP request. You should receive this request in an actual controller method which you can call multiple times with multiple different requests. By forcing your controller to resolve session or auth information in the constructor you are now forcing your entire controller to ignore the actual incoming request which can cause significant problems when testing, etc.
This worked for me:
public function __construct()
{
// No session access from constructor work around
$this->middleware(function ($request, $next) {
\Request::getSession()->getId();
return $next($request);
});
}
As stated in the documentation, the auth() function can be used instead of the Auth facade, returns an authenticator instance and can be used like this:
auth()->check()
auth()->guest()
auth()->id()
auth()->user()
As you can see - I can chain ->check() or ->guest() or ->id() or ->user().
Is it possible to add a new custom function that can be also chaned? For example, somethingNew() that can be used like this:
auth()->somethingNew() // returns boolean
The auth helper simply returns the instance of the guard being used. You can implement custom guards which would expose your somethingNew method.
Configure the application to use your new guard in config/app.php and enjoy calling auth()->somethingNew().
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');
I have one ProjectController with function getAll(). In getAll() I use eloquent ModelName::get() to fetch all projects and assigned to one variable $projectList.
I register one after middleware. In that middleware, I want to do some data filter on $projectList which I get in the controller.
Please help me how can I do that?
I am trying to understand how the Yii pagination for posts works in the Blog demo app and I see this request in the firebug console:
http://localhost/blog/index.php/post/index?ajax=yw0&Post_page=2
The function 'actionIndex' in the PostController does not seem to use the $_GET params. Where does the magic happen?
For such things you should check the source.
The index function would have a CActiveDataProvider whose fetchData function does this work.
Basically a CListView, or a CGridView calls the getData function of a data provider, which calls fetchData (say of CActiveDataProvider), which in turn calls CPagination's applyLimit, which calls getOffset, and this function calls getCurrentPage:
if(isset($_GET[$this->pageVar])) // this is where the $_GET is used