How to check an Ajax request in cakephp? - ajax

How to check an Ajax request in cakephp?

Depends on the version of cake.
1.3.x:
$this->RequestHandler->isAjax();
2.x || 3.x
$this->request->is('ajax');

You need to enable the RequestHandler component
var $components = array('RequestHandler');
Then you check if its an ajax request in your controllers with:
$this->RequestHandler->isAjax()
You can find more information about the RequestHandler component here

this question is an older one but just in case anyone comes across this like me and uses CakePHP 2:
RequestHandler::isAjax() is deprecated, use the Request Object's $this->request->is('ajax');
More info here

Without the use of components you can use something like this:
$this->params['isAjax'];
This will return a bool.

this is the way whisch is described is doc.I have been using since i started using cakephp
if($this->RequestHandler->isAjax()){
//
}

If you simply want to check the function of the php side, try:
$this->log('some debug',LOG_DEBUG);
then check app/tmp/logs/debug.log.

Related

Laravel 5 -- Redirect in Controller is not working in AJAX call

Important: this comes from ajax call.
Everything works perfectly except:
use Illuminate\Routing\Redirector;
public function my_call() {
return redirect()->route('page-1');
}
Throws 500 error.
return view('page-1') works without problems.
Maybe anybody sees what I am doing wrong?
Thank you.
Do you have an actual route with the name that you are using, you cannot redirect to a view as these are returned by routes
Apperantly you cannot do this on server side if you do AJAX call:/
Here an explanation: https://laracasts.com/discuss/channels/vue/redirect-after-ajax-post-request
It's for vue but I believe it applies universally for ajax.

Pass filtered data to Datatables v8 on Laravel v5.6

I have an integration with laravel datatables with elasticsearch. For the previous versions, this code was solving the issue.
return value(new CollectionEngine(collect($data), new DatatablesRequest()))
->setTotalRecords($total)
->make‌​(true);
Right now with the new version of datatables (v8) the constructor for collectionEngine has changed and no longer receives a second parameter with the request and the Yajra\DataTables\Request class has be removed as well
Still looking for a solution but with no success on this. How can I make this integration the same way I was doing it before.
Ok, didn't checked the correctly how the constructor was for the collection engine. This has been changed so it get the app request automatic so no need to be injecting new request to it. Only needs to include ->skipPaging() to the datatable.
$response = value(new CollectionEngine($data))
->skipPaging()
->make()

Session return null anyway

My project is using three different services and now I never can get sessions values, I've tried the laravel site tutorial and fallowing link question :
Laravel - Session returns null
But none of them worked!
In the first, i used this library:
use Session;
In a controller class :
$result = SocketHelper::sendRequest($req);
Session::put('uuid', $result->uuid);
Session::save();
Session::put('userId', $result->userID);
return Redirect::route('login_step_two');
In an other method :
$uuid = Session::get('uuid');
$userId = Session::get('userId');
But these are null! does I have to use cookie?
I recently upgrade to laravel 5.4
Please help me! It's made me confused!
Thanks.
Try saving Session explicitly like this, give it a try it worked for me, hope same for you.
Session::put('test_session', 'test message');
Session::save();
And retrieve it like this
echo Session::get('test_session');
And forget it like this:
Session::forget('test_session');
Session::save();
I understood the null result was becouse I was posting the value of $request to an other template and it was changed it the way :))) !
so easy to know !
Have you properly upgraded to laravel 5.4?
Laravel Docs
Sessions
Symfony Compatibility
Laravel's session handlers no longer implements Symfony's SessionInterface. Implementing this interface required us to implement extraneous features that were not needed by the framework. Instead, a new Illuminate\Contracts\Session\Session interface has been defined and may be used instead. The following code changes should also be applied:
All calls to the ->set() method should be changed to ->put(). Typically, Laravel applications would never call the set method since it has never been documented within the Laravel documentation. However, it is included here out of caution.
All calls to the ->getToken() method should be changed to ->token().
All calls to the $request->setSession() method should be changed to setLaravelSession().
Do you still have the rights to write session files in php session directory ?
Check the path returned by session_save_path() and check if your php user has the rights to write in it, check also if there is files in the directory and what are their creation date.

Changing The Laravel Passport/OAuth-Server Responses

I'm building an API in Laravel 5.4, using Laravel Passport 3 for authentication. All of my API methods return a set of values that are always returned, success, errors (if there are any errors) etc.
I've changed the response of a \Illuminate\Auth\AuthenticationException throws, to fit with the rest of my app, however I'm not sure how to change the response of various token grant responses, without doing something horrible like editing the vendor files.
I think you can use middleware to change your response.
From laravel documentation:
Before & After Middleware
Whether a middleware runs before or after a
request depends on the middleware itself.
You can capture the response and re-format the response.
You can use laravel's setContent method to set the content in response. Check here.
What you are trying to do here is not supported by the library, so whatever you do will be hacky and will probably break the compatibility with future versions of laravel/passport.
In my opinion, you can only choose between those 2 options:
Instead of declaring passport routes (Passport::routes()) you can declare equivalent routes to your custom methods. Those method internally calls Passport classes and methods, handling passport returning values before returning them to the user. It requires a lot of digging into passport code but, at the same time, if you only add some fields (success or error) you should be able to update your code without too much effort when updating the library.
Fork laravel/passport and modify it to suit you needs. This solution in not as messy as the first, but a merge with new versions of passport in the future will probably be hard.
Of course, both are not great solutions. Keeping the standard passport responses or use a more suitable library are better options: I assume they are not feasible if you are asking.
Another way - create proxy routes for your purposes.
Route::post('custom-auth/token', function (Request $request) {
$proxy = Request::create('oauth/token', 'POST', $request->request->input());
$response = app()->handle($proxy);
return responseCallback($response);
});
Where responseCallback your custom response modificator function.

Can you retrieve your Skype status using JSONP?

Does anyone know of a URL to get your Skype status using JSONP?
I've only found an XML status URL so far (http://mystatus.skype.com/username.xml).
(I'm trying to query Skype using AJAX. Yes, I could use a server-side proxy script to beat the cross-domain limits, but a direct call would be awesome.)
Simon.
Well apparently you can get a text-only version of the status by changing the extension to .txt:
http://mystatus.skype.com/username.txt
It will return "Online" or "Offline". About the Cross-domain AJAX, you can only do it via server and direct call is definitely not allowed.
You might change the headline to 'JSONP' instead of JSON. That's what you want.
JSONP hijacks cross domain fetches like this to work, without server proxies, by carrying the data in fetches. It's like the most hackish useful technology I come to mind, right now. :)
I nagged Skype about this - the easiest way out would be for their servers to have an official, documented JSONP interface. I hope they'll do that.
In the mean time, this is how I got the problem solved:
Placed this PHP script on my server, alongside the usual HTML: http://benalman.com/projects/php-simple-proxy/
Edited the configuration of it like so:
$enable_native = true;
$valid_url_regex = '/^http:\/\/mystatus\.skype\.com\/myuserid.*/';
This allows it to fetch (via curl running on the server) the mystatus.skype.com/myuserid.num (or .txt) information.
Fetching from JS with URL:
ba-simple-proxy.php?url=http%3A%2F%2Fmystatus.skype.com%2Fmyuserid.num&mode=native&full_status=1
That's it. Pheeew... :)
Also you can retrieve it using PHP
function getSkypeStatus($username) {
$data = file_get_contents('http://mystatus.skype.com/' . urlencode($username) . '.xml');
return strpos($data, '<presence xml:lang="en">Offline</presence>') ? 'Offline' : 'Online';
}
OR
function getSkypeStatus($username) {
$data = file_get_contents('http://mystatus.skype.com/' . urlencode($username) . '.xml');
preg_match('#<presence xml:lang="en">(.*?)</presence>#i', $data, $match);
return isset($match[1]) ? $match[1] : 'Error retrieving status';
}
Cheers!
Thanks to Bradgrafelman from - http://www.phpbuilder.com/board/showthread.php?t=10361050

Resources