laravel: routing any ajax request

Discussion for various published PHP frameworks, including Zend Framework, CodeIgniter, Kohana, CakePHP, Yii, Symfony, and others.

Moderator: General Moderators

Post Reply
publicGenome
Forum Contributor
Posts: 110
Joined: Thu Apr 16, 2015 7:55 am

laravel: routing any ajax request

Post by publicGenome »

Hi There,

I'm working on laravel 4.2.
Here, I've been processing ajax request from route as:

Code: Select all


Route::group(['prefix' => 'illumina', 'namespace' => 'Controllers\\illumina','before' => 'pims_auth'], function() {

	if(Request::ajax()){
		
		Route::get('illumina_xhr_get_samples','mainIllumina@get_illumina_samples');
		Route::post('submit_isolate_analysis','mainIllumina@submit_isolate_analysis'); //called from js->illumina->grid.js		
	}

});

URLs are as:
url:'illumina/illumina_xhr_manage_samples',
url:'illumina/illumina_xhr_get_samples'
And so on. That is prefixed with 'illumina/'

However, this above route is starting to get clumsy. If I've 10 such request, I'm dumping and making routes php page a pure mess.
I'd like to so something:

Code: Select all

if(Request::ajax()){

Route::match(array('GET','POST') do something);

//pass any request to mailIllumina controller's process function
	}
I've following issues:
1) here I'd like to send URL to the process_ajax function too


I'm unable to get this into working.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: laravel: routing any ajax request

Post by Celauran »

Explicit routing is far better than implicit. Your routes file should be a gateway into your application; anyone not familiar with the project should be able to gain a decent understanding of what's going on simply by looking at it. I commonly have routes files that are hundreds of lines long, and I think that's a good thing.
I've following issues:
1) here I'd like to send URL to the process_ajax function too
This, however, suggests you're trying to avoid some possible duplication, which is good.

Can you elaborate on what you're trying to accomplish (and, if applicable, what you're trying to avoid) and we'll see what we can come up with.
publicGenome
Forum Contributor
Posts: 110
Joined: Thu Apr 16, 2015 7:55 am

Re: laravel: routing any ajax request

Post by publicGenome »

Celauran wrote:Explicit routing is far better than implicit. Your routes file should be a gateway into your application; anyone not familiar with the project should be able to gain a decent understanding of what's going on simply by looking at it. I commonly have routes files that are hundreds of lines long, and I think that's a good thing.
I've following issues:
1) here I'd like to send URL to the process_ajax function too
This, however, suggests you're trying to avoid some possible duplication, which is good.

Can you elaborate on what you're trying to accomplish (and, if applicable, what you're trying to avoid) and we'll see what we can come up with.
Hi Celauran,
Thanks much for your prompt and supportive reply. :)
A brief about application:

Have a nav bar on left. Created by a legacy code.
I added another category in nav-bar. illumina (please see attached screen shot nav_bar)
Illumina category has couple of interfaces:

1) samples - display a grid with all samples information.
sample here refer to DNA/genome collected. Say, nose swab collected in Arizona. (please see attached grid sample screen shot)
User can select rows, and send them to group analysis.

Here, the gird is generated as:

Code: Select all

	$("#grid_illumina_samples").jqGrid({
		datatype: "json",
		url:'illumina/illumina_xhr_get_samples',
               mtype: "GET",
//so on and so forth..
});
Request goes to routes.php from there, go to controller, and model.. send rows. and populate the grid.

On selecting samples from the above Gird, user can send these samples to create a bundle (a group) .. My code for this looks like:

Code: Select all

	function submit_form(analysisName,sample_id){
		$.ajax({
			url:'illumina/submit_isolate_analysis',
			type: 'POST',
//so on and so forth..
			}
		});
Again this goes to routes - controller and model..Dump the analysis name in database, etc.

---- ---------------------------------------

2) Now manage analysis:
This has a different grid (same theme, but different entries), but different features (button at the bottom): Group for analysis, manage tools, etc

And for each such GET/POST request I'm adding another line in my routes as shown in the post #1 .
---- ---------------------------------------

3, 4, 5: more interfaces, different grid, another features..

---- ---------------------------------------
I'm a noob, and without any guidance and experience in design of code, and layouts.

I feel that I'm just making routes page a mess, thus I thought if I can club all ajax, and send them to a mainIllumina's process_ajax function, from there things can be handled as they have been.

I hope I made a little sense of my dilemma here.
Thank you again for your time. :)
Attachments
samples_grid.png
nav_bar.png
nav_bar.png (10.12 KiB) Viewed 8173 times
Last edited by publicGenome on Tue Dec 08, 2015 9:25 am, edited 2 times in total.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: laravel: routing any ajax request

Post by Celauran »

If your POST requests all submit different payloads and your GET requests all return different payloads, I'd leave them as they are. A descriptive routes file is a good thing. If you've got a number of routes that are basically the same, you can route those to a common controller action and go from there. Keeping your routes grouped and prefixed like you're already doing helps keep things nice and neat.
publicGenome
Forum Contributor
Posts: 110
Joined: Thu Apr 16, 2015 7:55 am

Re: laravel: routing any ajax request

Post by publicGenome »

Celauran wrote:If your POST requests all submit different payloads and your GET requests all return different payloads, I'd leave them as they are. A descriptive routes file is a good thing. If you've got a number of routes that are basically the same, you can route those to a common controller action and go from there. Keeping your routes grouped and prefixed like you're already doing helps keep things nice and neat.
Dear Celauran,

Yes, I've GET and POST requests with different payloads.
Thank you for clearing clouds over routes php file. That helps perfectly. :)

I'd get the thing going, then. :D
Post Reply