Question

I have a filter setup that runs a function which checks if the user session is present on certain actions, like so;

<cffunction name="init">

        <cfset filters(through="checkLogin", except="login,register,signin,create,home,profile") />

    </cffunction>

The problem is, these are the action names...which conflict from other controllers I have.

For example, I have 2 controllers 'user' and 'link'. Each of these has an action called create, so that my URL's are like so:

/user/create/ /link/create/

How can the filter know with which controller to associate it with? Is there a way to prefix certain 'actions' in the 'except' clause with the controller name too?

For example, maybe something like:

<cffunction name="init">

        <cfset filters(through="checkLogin", except="user/login,user/register,user/signin,link/create,main/home,user/profile") />

    </cffunction>

I remember trying this but it didn't work and borked.

Hope you understand what I'm saying here. I don't want to have to name every action in separate controllers totally unique names.

Thanks, Michael.

Was it helpful?

Solution

You can use basic inheritance to accomplish this:

<!--- controllers/Controller.cfc --->
<cffunction name="init">
    <cfargument name="checkLoginExcept" type="string" required="false" default="">

    <cfset filters(through="checkLogin", except=arguments.checkLoginExcept)>
</cffunction>

Then in any child controller (user, for example), you can specify which actions to exclude. This works well because the parent controller should really know nothing about its children. It implements what it cares about and nothing else.

<!--- controllers/User.cfc --->
<cffunction name="init">
    <cfset super.init(checkLoginExcept="login,register,signIn,profile")>
</cffunction>

If another child always wants for checkLogin to run, then it shouldn't pass a value for checkLoginExcept:

<!--- controllers/Foo.cfc --->
<cffunction name="init">
    <cfset super.init()>
</cffunction>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top