howto use fatfree php framework without rewrite engine - .htaccess

I'm not able to use an htaccess for my project therefore clean URLs doesn't work for routing. Is there a way to to use query params instead?
example
myf3project.net/user/show isn't possible. Instead I want to do something like this:
myf3project.net?path=user/show or
myf3project.net?view=user&action=show

You can also overwrite the $_SERVER['REQUEST_URI'] for this purpose:
$f3->route('GET /user', function ($f3) { echo "user"; });
$f3->route('GET /user/#action',function($f3,$params){
echo "user->".$params['action'];
});
if ($f3->exists('GET.path',$path))
$f3->URI = $f3->BASE.'/'.$path;
$f3->run();
and then just open
http://myf3project.net?path=user/show

Do you mean that mod_rewrite is not available? That's a problem indeed. You can however call $f3->mock to the rescue.
Here's a quick example:
$f3 = require('lib/base.php');
// Homepage
$f3->route('GET /',function($f3){
echo 'This is home. '.
'Click here to see the contact form.';
});
// Contact form
$f3->route('GET|POST /contact',function($f3){
echo 'This is the contact form. '.
'Click here to go back home.'.
'<form method="post" action="">'.
'<textarea name="message"></textarea>'.
'<button>Send message</button></form>';
if ($f3->VERB=='POST')
echo 'Submitted message:<br>'.
nl2br($f3->get('POST.message'));
});
// Workaround here: $f3->mock is used instead of $f3->run
$path=$f3->get('GET.path');
if (#$path[0]!=='/')
$path='/';
$f3->mock($f3->VERB.' '.$path,$_POST);

It's still kinda possible, unfortunately you have to build most on your own.
Very basic example:
<?php
$f3 = require 'lib/base.php';
$f3->route('GET /', function() use($f3) {
switch($f3->get('view')) {
case 'user':
// do something
break;
case 'admin':
// do something
break;
}
});
The "/" route should work even without .htaccess.

Based on #xfra35's answer, I propose a solution that will allow you to have paths like this:
/index.php/foo/bar
Instead of:
/index.php?path=/foo/bar
But you still can use query variables (like /index.php/foo/bar?test=123) and get them using $_GET['test'] or $f3->get('GET.test'):
Just replace your $f3->run(); with:
$path=$_SERVER['PATH_INFO'];
if (#$path[0]!=='/')
$path='/';
$f3->mock($f3->VERB.' '.$path.'?'.$_SERVER['QUERY_STRING'],$_POST);

Related

Passing JavaScript variable into snippet

I'm working on a search form for my ModX application that is consisted of a chunk and a snippet. What I'm trying to achieve is to pass what was entered into the search box into a javascript variable and then pass it to my snippet, however, the snippet receives the literal text, and not the value that I enter into the parameter when I call it.
I don't know if what I'm attempting is possible in ModX or if I need to take a different approach, but I would be hugely thankful for anyone who can provide any insight.
Chunk:
<script>
$('.search-btn').click(function() {
var search = $('.search-entry').val();
[[showSearchResults? &q=`search`]]
});
</script>
Snippet:
<?php
$search = $modx->getOption('q', $scriptProperties);
echo $search; // this always prints "search"
?>
I doubt that this code makes sense:
<script>
$('.search-btn').click(function() {
var search = $('.search-entry').val();
[[showSearchResults? &q=`search`]]
});
</script>
The snippet call returns the result of snippet's execution with param q always equal to the string 'search' in your case and finally on your page you will have something like this:
<script>
$('.search-btn').click(function() {
var search = $('.search-entry').val();
'search' // assuming your snippet just returns what has been passed to it.
});
</script>
In order to accomplish your task you can use a simple trick. Call your snippet like this:
[[!yourSnippet? &yourVar=`[[!#POST.yourVar]]` ]] // or GET
Lets say this snippet call is located on a page accessible via url /test/ on your server. So, now you just have to send the parameters you collected from your search form using AJAX to the /test/ page where your snippet is:
var yourVar = $('.search-entry').val();
$.ajax({
type: "POST",
url: "/test/",
data: {yourVar: yourVar},
success: success,
dataType: "html"
});
Hope it helps :)
PS If you want to search Resource content and TV content, I can highly recommend an extra called SimpleSearch.

how to modify the pagination rules by WP_Query?

I want to do the pagination for my custom post type "Employes", but if I use the WP_Query, I can use the attribute post_per_page, but the URL changed like this.../page/2. I want the URL changes by this way ...#2, because the former way I can't use tab content, Who can help me? Thank you so much!
Use this:
function custom_pagination_base() {
global $wp_rewrite;
$wp_rewrite->pagination_base = '{pagination-url-here}';
$wp_rewrite->flush_rules();
}
add_action( 'init', 'custom_pagination_base' );
Paste this code in theme function.php
{pagination-url-here} replace your uri segment

Generating query strings in Laravel 4

I have a named route
Route::get('login/facebook', array(
'as' > 'login.facebook.authorise',
'uses' => 'AuthController#getFBAuthorise'
));
and want to produce a URL pointing to it with a query string appended.
For example:
URL::route('login.facebook.authorise', array("next"=>'/dashboard/')
would produce
/login/facebook?next=/dashboard/
Is something like this built in to the framework? If not, what is the best way to try and do this?
Thanks.
The URL helper can do this.
http://laravel.com/docs/helpers#urls
echo link_to_route('route.name', $title, $parameters = array(), $attributes = array());
EDIT: To get only the URL :
http://laravel.com/docs/routing#named-routes
$url = URL::route('route.name', $parameters);
I am a bit late to the party
Just dove into the classes and methods for building urls and you can do this:
link_to_route('login.facebook.authorise','Facebook Login',array('next'=>'/Dashboard/'))
So. the array will always fallback to query params unless the names are declared in the route.
If the use case for this is redirecting to a page after logging in then you could use
Redirect::intended('dashboard');
As seen here http://laravel.com/docs/security#authenticating-users
Sorry if that's not what you're trying to achieve.

MODx caches what it shouldn't

I'm working with MODx revo. I wrote a snippet called putBoxId with the following content:
<?php
$id = isset($_GET['id']) ? $_GET['id'] : null;
if (!is_null($id)) {
return $modx->getChunk($tpl, array('id' => $id));
}
return '';
I use it like this: [[~3[[!putBoxId? &tpl='boxIdUrl']] ]] (with backticks, of course), where boxIdUrl is the chunk with the following content:
? &id=`[[+id]]`
The problem is, for some reason it gets cached. I tried putting '!' In all combinations, still gets cached. How can this be fixed?
The [[~3 is being cached, so your putBoxId is actually called only the first time.
In Revo - any *[[* (tag) can start with a ! (non-cacheable flag). So, in your case - [[!~3[[!putBoxId? &tpl='boxIdUrl']] ]] (note: there's a typo here and in your original question, see comment below. this should work: [[~3]][[!putBoxId? &tpl='boxIdUrl']])
more info here
Even better - unless there's a good reason, get rid of that chunk, as the $modx->getChunk call wouldn't be cached in your scenario (goes to db to get template, etc...).
Do it all in the snippet itself using modx->makeUrl (see link for more options)
<?php
$resourceId = $modx->getOption('resourceId', $properties, $modx->resource->get('id')); // get resourceId from snippet, default to current
$args = (!empty($_REQUEST['id']))? array('id'=>$_REQUEST['id']) : '';
return $modx->makeUrl($resourceId, '', $args);
Call like this:
[[!putBoxId]] or [[!putBoxId? &resourceId=`3`]]

Chrome Extension iframe dom reference disqus

How do I get a reference to the dom of a cross domain iframe/frame?
I want to do some stuff to disqus comments with an extension.
My manifest has the following:
"all_frames": true,
"matches": ["*://*.disqus.com/*","*://disqus.com/*", "http://somesite.com"]
I am not trying to communicate outside of the frame - that is the js will take care of the work without needing to 'tell' me anything.
all_frames should inject the listed js files into every frame, no?
When I do this:
if (window != window.top){
alert('In an IFRAME: ' + window.location.href);
}
...I get the expected disqus URLs.
But when I do this:
var btnCommentBlock = document.getElementsByClassName('dsq-comment-buttons');
alert('btnCommentBlock length: ' + $(btnCommentBlock).length);
...I get 0 for length.
I updated my answer to Javascript to access Disqus comment textbox?
Basically, Disqus changed the selector. They no longer use textarea, they use contenteditable divs.
Something like this should work:
// We just need to check if the IFrame origin is from discus.com
if (location.hostname.indexOf('.disqus.com') != -1) {
// Extract the textarea (there must be exactly one)
var commentBox = document.querySelector('#comment');
if (commentBox) {
// Inject some text!
commentBox.innerText = 'Google Chrome Injected!';
}
}
Source Code:
https://gist.github.com/1034305
Woohoo! I found the answer on github:
https://gist.github.com/471999
The working code is:
$(document).ready(function() {
window.disqus_no_style = true;
$.getScript('http://sitename.disqus.com/embed.js', function() {
var loader = setInterval(function() {
if($('#disqus_thread').html().length) {
clearInterval(loader);
disqusReady();
}
}, 1000);
});
function disqusReady() {
//whatever you can imagine
}
});
I put this in the disqusReady() function:
var aTestHere = document.getElementsByClassName('dsq-comment-body');
alert(aTestHere[0].innerHTML);
...and got back the innerHTML as expected.
Mohamed, I'd really like to thank you for taking the time to interact with my question. If you hadn't posted that link to github there's no telling when if ever I'd have figured it out or found the other code.
edit: After a few minutes of experimenting it looks like it is not necessary to call getScript so you should be able to comment that out.
Also unnecessary is window.disqus_no_style so I commented that out too.
I'll experiment some more and update the answer later. One of those two things prevented me from being able to actually post a comment at the disqus site I use. //them out still allows access to the dom and the ability to post.

Resources