You are viewing a read-only archive of the Blogs.Harvard network. Learn more.

Yii forward with params

Yii’s forward doesn’t work with passing params to actions. I.e. This doesn’t work: $this->forward(“/mycontroller/myaction/1/2”);

Because of the way urlmanager works I made the decision early on to have all action parameters take on the var names $id and $id2. So in the main config:

'components'=>array(
			// uncomment the following to enable URLs in path-format
			'urlManager'=>array(
				'urlFormat'=>'path',
				'rules'=>array(
					'<controller:\w+>/<id:\d+>'=>'<controller>/view',
					'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
					'<controller:\w+>/<action:\w+>/<id:\d+>/<id2:\d+>'=>'<controller>/<action>',
					'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
					'<controller:\w+>/<action:\w+>/<cact:\w+>'=>'<controller>/<cact>',
				),
			),

So I used Controller.php in the components directory to override the forward method like so:

	 /**
	 * Overloading CController::forward
	 * added the routeArr lines so the ids are passed
	 */
	public function forward($route,$exit=true){
		//error_log("Controller::forward");
		$routeArr = explode("/", $route);
		if(isset($routeArr[3]))
			$_GET['id'] = $routeArr[3];
		if(isset($routeArr[4]))
			$_GET['id2'] = $routeArr[4];

		parent::forward($route, $exit);

	}

And that’s it.

Posted in ATG, Quizmo, Yii. Tags: , . Comments Off on Yii forward with params »

Yii Javascript redirect: jsredirect

One of the platforms I have to develop for is iSites. This is a Harvard grown LMS that is complicated and annoying, but functional.

Since I have to develop tools that fit within this framework I have to work around its limitations. One of the simpler to understand limitations is the idea of not having control of the headers sent. Since by the time it gets to my tool, the page has already started loading, output has already been sent, so doing a redirect with PHP’s header function is impossible.

Yii has 2 ways to forward things through the controller. redirect and forward. Redirect uses header and forward doesn’t change the URL. So the best way to forward within isites is to use a js forward. I.e. document.location = “www.google.com”

So in the controller.php in components which extends CController I added a method jsredirect:
 https://github.com/jazahn/Quizmo/blob/ma…

	protected function jsredirect($url){
		// set the redirect in a session
		Yii::app()->session['jsredirect'] = $url;

		// forward to the jsredirect action
		$this->forward('/site/jsredirect');
	}

This just sets a session var for the redirect and forwards to site/jsredirect so in SiteController.php I have

public function actionJsredirect(){
		
		if(isset(Yii::app()->session['jsredirect'])){
			$this->render('jsredirect',array(
				'url'=>Yii::app()->session['jsredirect'],
			));
		}
		
	}

And then in the jsredirect template file we have

<script>
window.location.replace("{$url}");
</script>
Posted in ATG, Javascript, PHP, Yii. Tags: , , , . Comments Off on Yii Javascript redirect: jsredirect »

Using git with svn

If you have the choice, don’t. Pick one and stick with it. My personal preference at this time is git because it facilitates smaller commits — but who cares what I prefer, you’re probably tied to a versioning system based on what someone long before you decided to use.

I for one am tied to svn for doing deploys, but since I’m gaga over git and doing things in the open, I want to use git on a day to day basis and just use svn for deploys.

This sucks, but if you’re going to do it, you shouldn’t be a douche and add your .git directory to svn. That was my first attempt, even though I knew it was bad, I wanted to see the performance hit on svn firsthand I guess.

So I have the deployment related files in svn, so git is free of any environment specific files, like database configs.

So I do all development in git with “.svn” in the .gitignore. That part is easy. Getting svn to ignore git is a little more annoying, especially if you have multiple submodules. So when you’re ready, you add everything to svn, then rm the .git files. It’s better to rm them with the –keep-local flag.

If you delete the .git files because you forgot to –keep-local, or, more likely you’ve had to blow away your development and bring it back via svn, then you have to restore the .git files. Restoring them isn’t so bad:

git init
git remote add origin  git at github.com/your_project.git
git pull origin master

Posted in ATG, Git, SVN, Version Control. Tags: , . Comments Off on Using git with svn »

SVN ignoring Git

The first thing I had to deal with was ignoring multiple files in one directory. I’ve always known

svn propset svn:ignore something.txt .

But if you then

svn propset svn:ignore something_else.txt .

it will lose the first ignore. The way to do multiple ignores is annoying:

svn propset svn:ignore "something.txt
> something_else.txt" .

It requires the “s and you have to separate the files with a newline. That’s silly.

So the right way seems to be with an svn ignore file. I created a file “.svnignore”:

.git
.gitignore
.gitmodules

(it doesn’t have to be called .svnignore, that just keeps it straight with me exactly what it is)

Then the following command needs to be run:

svn -R propset svn:ignore -F .svnignore .

and this will recursively run through every directory in “.” and ignore every pattern in the file. The only issue is any time new directories are added that contain git (i.e. submodules) the command will have to be re-run.

Posted in Git, SVN, Version Control. Tags: , , . 2 Comments »

JavaScript is a Trap

Note: opinion

JavaScript is the most important part of web development. It’s what separates web applications from a collection of web pages.

The trap is one that I’ve fallen into multiple times and I’ve seen other developers fall into, unable to help them. They have these grandiose ideas and the way they see it working is with a mass of JavaScript. So they get this ridiculous mess of code, and they may not admit it’s a mess, but the trap with JavaScript is there is no way to write a lot of it without it becoming a mess.

One of my greatest offenses in this has been my use of extjs. This is a js framework that requires writing the code in a specialized fashion. You end up with classes that sometimes look clean but the more you stray away from exactly what the code was specialized for, the more it becomes a mess. And this happens fast.

Sometimes it’s a necessary mess. Innovation is often a mess. The problem is differentiating innovative in terms of JavaScript and otherwise innovative. Lots of people are doing innovative things, but it’s very rarely innovative in terms of the JavaScript.

So what’s the solution?

In my opinion, JavaScript in general should be minimal. 99.9% of what needs to be done for any web application can be done with jQuery, underscore, jQueryUI, and not a lot of it. I don’t believe you should ever be writing more than what you can copy paste directly from the jquery documentation. And you can replace “jquery” with any major js library. Don’t go outside of what the library was designed for, you really don’t need it most of the time. Really.

Maybe I should update this to be more about how JavaScript Frameworks are the trap, but it doesn’t matter if you’re using a framework or not, the more you write, the shittier it becomes.

Posted in Javascript. Tags: , . 1 Comment »